JoinPoint 對象
JoinPoint對象封裝了SpringAop中切面方法的信息,在切面方法中添加JoinPoint參數,就可以獲取到封裝了該方法信息的JoinPoint對象.?
常用api:
方法名 | 功能 |
---|---|
Signature getSignature(); | 獲取封裝了署名信息的對象,在該對象中可以獲取到目標方法名,所屬類的Class等信息 |
Object[] getArgs(); | 獲取傳入目標方法的參數對象 |
Object getTarget(); | 獲取被代理的對象 |
Object getThis(); | 獲取代理對象 |
ProceedingJoinPoint對象
ProceedingJoinPoint對象是JoinPoint的子接口,該對象只用在@Around的切面方法中,?
添加了?Object proceed() throws Throwable //執行目標方法
?Object proceed(Object[] var1) throws Throwable //傳入的新的參數去執行目標方法
?
兩個方法.
Demo
切面類
@Aspect
@Component
public class aopAspect {/*** 定義一個切入點表達式,用來確定哪些類需要代理* execution(* aopdemo.*.*(..))代表aopdemo包下所有類的所有方法都會被代理*/@Pointcut("execution(* aopdemo.*.*(..))")public void declareJoinPointerExpression() {}/*** 前置方法,在目標方法執行前執行* @param joinPoint 封裝了代理方法信息的對象,若用不到則可以忽略不寫*/@Before("declareJoinPointerExpression()")public void beforeMethod(JoinPoint joinPoint){System.out.println("目標方法名為:" + joinPoint.getSignature().getName());System.out.println("目標方法所屬類的簡單類名:" + joinPoint.getSignature().getDeclaringType().getSimpleName());System.out.println("目標方法所屬類的類名:" + joinPoint.getSignature().getDeclaringTypeName());System.out.println("目標方法聲明類型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));//獲取傳入目標方法的參數Object[] args = joinPoint.getArgs();for (int i = 0; i < args.length; i++) {System.out.println("第" + (i+1) + "個參數為:" + args[i]);}System.out.println("被代理的對象:" + joinPoint.getTarget());System.out.println("代理對象自己:" + joinPoint.getThis());}/*** 環繞方法,可自定義目標方法執行的時機* @param pjd JoinPoint的子接口,添加了* Object proceed() throws Throwable 執行目標方法* Object proceed(Object[] var1) throws Throwable 傳入的新的參數去執行目標方法* 兩個方法* @return 此方法需要返回值,返回值視為目標方法的返回值*/@Around("declareJoinPointerExpression()")public Object aroundMethod(ProceedingJoinPoint pjd){Object result = null;try {//前置通知System.out.println("目標方法執行前...");//執行目標方法//result = pjd.proeed();//用新的參數值執行目標方法result = pjd.proceed(new Object[]{"newSpring","newAop"});//返回通知System.out.println("目標方法返回結果后...");} catch (Throwable e) {//異常通知System.out.println("執行目標方法異常后...");throw new RuntimeException(e);}//后置通知System.out.println("目標方法執行后...");return result;}
}
被代理類
/*** 被代理對象*/
@Component
public class TargetClass {/*** 拼接兩個字符串*/public String joint(String str1, String str2) {return str1 + "+" + str2;}
}
測試類
public class TestAop {@Testpublic void testAOP() {//1、創建Spring的IOC的容器ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:bean.xml");//2、從IOC容器中獲取bean的實例TargetClass targetClass = (TargetClass) ctx.getBean("targetClass");//3、使用beanString result = targetClass.joint("spring","aop");System.out.println("result:" + result);}
}
輸出結果
目標方法執行前...
目標方法名為:joint
目標方法所屬類的簡單類名:TargetClass
目標方法所屬類的類名:aopdemo.TargetClass
目標方法聲明類型:public
第1個參數為:newSpring
第2個參數為:newAop
被代理的對象:aopdemo.TargetClass@4efc180e
代理對象自己:aopdemo.TargetClass@4efc180e
目標方法返回結果后...
目標方法執行后...
result:newSpring+newAop
參考文章:?
http://blog.csdn.net/ochangwen/article/details/52557724?
http://blog.csdn.net/a9529lty/article/details/7031070