博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Aop温故
阅读量:4228 次
发布时间:2019-05-26

本文共 2152 字,大约阅读时间需要 7 分钟。

AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点对象,该类是JoinPoint的子接口。任何一个增强方法都可以通过将第一个入参声明为JoinPoint访问到连接点上下文的信息。我们先来了解一下这两个接口的主要方法:  1)JoinPoint   java.lang.Object[] getArgs():获取连接点方法运行时的入参列表;   Signature getSignature() :获取连接点的方法签名对象;   java.lang.Object getTarget() :获取连接点所在的目标对象;   java.lang.Object getThis() :获取代理对象本身; /** * 声明一个注解 */@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Action {    String name();} @Servicepublic class DemoAnnotationService {    @Action(name="注解式拦截的add操作")    public void add(){    }}   @Servicepublic class DemoMethodService {    public void add(){}} @Aspect@Component //配置为beanpublic class LogAspect {    @Pointcut("@annotation(com.wisely.aop.Action)")//申明一个切面,供重复使用    public void annotationPointCut(){}    /**     * joinPoint主要用于和被代理类交互,前面对该类有解释说明     * @param joinPoint     */    @After("annotationPointCut()")    public void after(JoinPoint joinPoint){        MethodSignature signature=(MethodSignature)joinPoint.getSignature();        Method method=signature.getMethod();        Action action=method.getAnnotation(Action.class);        System.out.println("注解式拦截 "+action.name());    }    @Before("execution(* com.wisely.aop.DemoMethodService.*(..))")    public void before(JoinPoint joinPoint){        MethodSignature signature=(MethodSignature)joinPoint.getSignature();        Method method=signature.getMethod();        System.out.println("方法规则式拦截,"+method.getName());    }} @Configuration//该类为配置类@ComponentScan("com.wisely.aop")//扫描包下所有使用@Service,@Component,@Repository,@Controller的类并注册为bean@EnableAspectJAutoProxy //该注解用于开启Spring对AspectJ的支持public class AopConfig {} public class Main {    public static void main(String[] args){        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(AopConfig.class);        DemoAnnotationService demoAnnotationService=context.getBean(DemoAnnotationService.class);        DemoMethodService demoMethodService=context.getBean(DemoMethodService.class);        demoAnnotationService.add();        demoMethodService.add();        context.close();    }}

转载地址:http://mojqi.baihongyu.com/

你可能感兴趣的文章
对象切割 - 常量引用传递
查看>>
北邮同学面经
查看>>
Effective C++条款16:成对使用new和delete时要采取相同形式
查看>>
sizeof与strlen
查看>>
一个递归+二分法的洗牌程序
查看>>
YUV格式注释
查看>>
一维、二维数组传参
查看>>
判断当前时间的下一秒是多少
查看>>
从文本文件中读取数据排序并输出到文本
查看>>
求一个整数数组中第二大的数
查看>>
删除一个链表中的节点
查看>>
计算机网络面试整理【转】
查看>>
cookie和session区别详解
查看>>
程序员失业第一步?斯坦福研究员用AI从编译器反馈中学习改Bug
查看>>
原创 | 电视广告流量预测中的“常识”陷阱,你掉进去了吗?
查看>>
DeepMind发布最新《神经网络中持续学习》综述论文!
查看>>
本科三篇顶会一作、超算竞赛冠军,2020清华本科特奖结果出炉
查看>>
多语言互通:谷歌发布实体检索模型,涵盖超过100种语言和2000万个实体
查看>>
你的房东可能正用AI筛查你的犯罪记录,决定要不要租房给你
查看>>
AI把爱豆变胖视频火遍B站,我们找到了背后的技术团队:你是怎么把刘亦菲变胖的?...
查看>>