码迷,mamicode.com
首页 > Windows程序 > 详细

Aop 中 JoinPoint等对象的用法Api

时间:2018-07-05 23:29:15      阅读:1484      评论:0      收藏:0      [点我收藏+]

标签:选择   自定义   逻辑   obj   target   family   run   aspect   api   

@Aspect 定义类为切入类

@Pointcut 声明一个切入策略供 @Before @After @ Around @ AfterReturning选择

@Before 被切入方法执行前执行

@After 被切入方法执行后执行

@Around 被切入方法前后都可以加入一些逻辑

@AfterReturning 被切入方法返回时执行

JoinPoint 加入这个参数可以获取被切入方法的名称和参数

JoinPoint 对象

Signature getSignature();//获取封装了署名信息的对象,在该对象中可以获取到目标方法名,所属类的Class等信息 (修饰符+包名+类名+方法名)

Object[] getArgs();//获取传入目标方法的参数对象

Object getTarget();//获取传入目标方法的参数对象

Object getThis();//获取代理对象

getSignature().getName();//获取方法名

ProceedingJoinPoint对象

只用在@Around的切面方法中,是JoinPoint的子接口

Object proceed() throws Throwable //执行目标方法 
Object proceed(Object[] var1) throws Throwable //传入的新的参数去执行目标方法 

Aspect类Demo

 1 @Aspect
 2 @Component
 3 public class aopAspect {
 4     /**
 5      * 定义一个切入点表达式,用来确定哪些类需要代理
 6      * execution(* aopdemo.*.*(..))代表aopdemo包下所有类的所有方法都会被代理
 7      */
 8     @Pointcut("execution(* aopdemo.*.*(..))")
 9     public void declareJoinPointerExpression() {}
10 
11     /**
12      * 前置方法,在目标方法执行前执行
13      * @param joinPoint 封装了代理方法信息的对象,若用不到则可以忽略不写
14      */
15     @Before("declareJoinPointerExpression()")
16     public void beforeMethod(JoinPoint joinPoint){
17         System.out.println("目标方法名为:" + joinPoint.getSignature().getName());
18         System.out.println("目标方法所属类的简单类名:" +        joinPoint.getSignature().getDeclaringType().getSimpleName());
19         System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName());
20         System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));
21         //获取传入目标方法的参数
22         Object[] args = joinPoint.getArgs();
23         for (int i = 0; i < args.length; i++) {
24             System.out.println("第" + (i+1) + "个参数为:" + args[i]);
25         }
26         System.out.println("被代理的对象:" + joinPoint.getTarget());
27         System.out.println("代理对象自己:" + joinPoint.getThis());
28     }
29 
30     /**
31      * 环绕方法,可自定义目标方法执行的时机
32      * @param pjd JoinPoint的子接口,添加了
33      *            Object proceed() throws Throwable 执行目标方法
34      *            Object proceed(Object[] var1) throws Throwable 传入的新的参数去执行目标方法
35      *            两个方法
36      * @return 此方法需要返回值,返回值视为目标方法的返回值
37      */
38     @Around("declareJoinPointerExpression()")
39     public Object aroundMethod(ProceedingJoinPoint pjd){
40         Object result = null;
41 
42         try {
43             //前置通知
44             System.out.println("目标方法执行前...");
45             //执行目标方法
46             //result = pjd.proeed();
47             //用新的参数值执行目标方法
48             result = pjd.proceed(new Object[]{"newSpring","newAop"});
49             //返回通知
50             System.out.println("目标方法返回结果后...");
51         } catch (Throwable e) {
52             //异常通知
53             System.out.println("执行目标方法异常后...");
54             throw new RuntimeException(e);
55         }
56         //后置通知
57         System.out.println("目标方法执行后...");
58 
59         return result;
60     }
61 }

 

Aop 中 JoinPoint等对象的用法Api

标签:选择   自定义   逻辑   obj   target   family   run   aspect   api   

原文地址:https://www.cnblogs.com/Eeexiang/p/9270713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!