JoinPoint的用法
生活随笔
收集整理的這篇文章主要介紹了
JoinPoint的用法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
JoinPoint 對(duì)象
JoinPoint對(duì)象封裝了SpringAop中切面方法的信息,在切面方法中添加JoinPoint參數(shù),就可以獲取到封裝了該方法信息的JoinPoint對(duì)象.?
常用api:
| Signature getSignature(); | 獲取封裝了署名信息的對(duì)象,在該對(duì)象中可以獲取到目標(biāo)方法名,所屬類的Class等信息 |
| Object[] getArgs(); | 獲取傳入目標(biāo)方法的參數(shù)對(duì)象 |
| Object getTarget(); | 獲取被代理的對(duì)象 |
| Object getThis(); | 獲取代理對(duì)象 |
ProceedingJoinPoint對(duì)象
ProceedingJoinPoint對(duì)象是JoinPoint的子接口,該對(duì)象只用在@Around的切面方法中,?
添加了?
Object proceed() throws Throwable //執(zhí)行目標(biāo)方法?
Object proceed(Object[] var1) throws Throwable //傳入的新的參數(shù)去執(zhí)行目標(biāo)方法?
兩個(gè)方法.
Demo
切面類
@Aspect @Component public class aopAspect {/*** 定義一個(gè)切入點(diǎn)表達(dá)式,用來(lái)確定哪些類需要代理* execution(* aopdemo.*.*(..))代表aopdemo包下所有類的所有方法都會(huì)被代理*/@Pointcut("execution(* aopdemo.*.*(..))")public void declareJoinPointerExpression() {}/*** 前置方法,在目標(biāo)方法執(zhí)行前執(zhí)行* @param joinPoint 封裝了代理方法信息的對(duì)象,若用不到則可以忽略不寫*/@Before("declareJoinPointerExpression()")public void beforeMethod(JoinPoint joinPoint){System.out.println("目標(biāo)方法名為:" + joinPoint.getSignature().getName());System.out.println("目標(biāo)方法所屬類的簡(jiǎn)單類名:" + joinPoint.getSignature().getDeclaringType().getSimpleName());System.out.println("目標(biāo)方法所屬類的類名:" + joinPoint.getSignature().getDeclaringTypeName());System.out.println("目標(biāo)方法聲明類型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));//獲取傳入目標(biāo)方法的參數(shù)Object[] args = joinPoint.getArgs();for (int i = 0; i < args.length; i++) {System.out.println("第" + (i+1) + "個(gè)參數(shù)為:" + args[i]);}System.out.println("被代理的對(duì)象:" + joinPoint.getTarget());System.out.println("代理對(duì)象自己:" + joinPoint.getThis());}/*** 環(huán)繞方法,可自定義目標(biāo)方法執(zhí)行的時(shí)機(jī)* @param pjd JoinPoint的子接口,添加了* Object proceed() throws Throwable 執(zhí)行目標(biāo)方法* Object proceed(Object[] var1) throws Throwable 傳入的新的參數(shù)去執(zhí)行目標(biāo)方法* 兩個(gè)方法* @return 此方法需要返回值,返回值視為目標(biāo)方法的返回值*/@Around("declareJoinPointerExpression()")public Object aroundMethod(ProceedingJoinPoint pjd){Object result = null;try {//前置通知System.out.println("目標(biāo)方法執(zhí)行前...");//執(zhí)行目標(biāo)方法//result = pjd.proeed();//用新的參數(shù)值執(zhí)行目標(biāo)方法result = pjd.proceed(new Object[]{"newSpring","newAop"});//返回通知System.out.println("目標(biāo)方法返回結(jié)果后...");} catch (Throwable e) {//異常通知System.out.println("執(zhí)行目標(biāo)方法異常后...");throw new RuntimeException(e);}//后置通知System.out.println("目標(biāo)方法執(zhí)行后...");return result;} }被代理類
/*** 被代理對(duì)象*/ @Component public class TargetClass {/*** 拼接兩個(gè)字符串*/public String joint(String str1, String str2) {return str1 + "+" + str2;} }測(cè)試類
public class TestAop {@Testpublic void testAOP() {//1、創(chuàng)建Spring的IOC的容器ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:bean.xml");//2、從IOC容器中獲取bean的實(shí)例TargetClass targetClass = (TargetClass) ctx.getBean("targetClass");//3、使用beanString result = targetClass.joint("spring","aop");System.out.println("result:" + result);} }輸出結(jié)果
目標(biāo)方法執(zhí)行前... 目標(biāo)方法名為:joint 目標(biāo)方法所屬類的簡(jiǎn)單類名:TargetClass 目標(biāo)方法所屬類的類名:aopdemo.TargetClass 目標(biāo)方法聲明類型:public 第1個(gè)參數(shù)為:newSpring 第2個(gè)參數(shù)為:newAop 被代理的對(duì)象:aopdemo.TargetClass@4efc180e 代理對(duì)象自己:aopdemo.TargetClass@4efc180e 目標(biāo)方法返回結(jié)果后... 目標(biāo)方法執(zhí)行后... result:newSpring+newAop參考文章:?
http://blog.csdn.net/ochangwen/article/details/52557724?
http://blog.csdn.net/a9529lty/article/details/7031070
總結(jié)
以上是生活随笔為你收集整理的JoinPoint的用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 什么是P2P
- 下一篇: layui 渲染select下拉选项 ,