色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

基于Spring AOP @AspectJ進(jìn)階說明

瀏覽:2日期:2023-07-26 09:26:52

@AspectJ可以使用切點(diǎn)函數(shù)定義切點(diǎn),我們還可以使用邏輯運(yùn)算符對(duì)切點(diǎn)進(jìn)行復(fù)核運(yùn)算得到復(fù)合的切點(diǎn),為了在切面中重用切點(diǎn),我們還可以對(duì)切點(diǎn)進(jìn)行命名,以便在其他的地方引用定義過的切點(diǎn)。

當(dāng)一個(gè)連接點(diǎn)匹配多個(gè)切點(diǎn)時(shí),需要考慮織入順序的問題,此外一個(gè)重要的問題是如何再增強(qiáng)中訪問連接點(diǎn)上下文的信息。

Waiter接口:

package com.yyq.aspectJAdvanced;public interface Waiter { void greetTo(String name); void serveTo(String name);}

NaiveWaiter實(shí)現(xiàn)類:

package com.yyq.aspectJAdvanced;public class NaiveWaiter implements Waiter { @Override public void greetTo(String name) { System.out.println('NaiveWaiter:greet to ' + name + '...'); } @Override public void serveTo(String name) { System.out.println('NaiveWaiter:serving to ' + name + '...'); } public void smile(String clientName,int times){ System.out.println('NaiveWaiter:smile to '+clientName+ times+'times...'); }}

NaughtyWaiter實(shí)現(xiàn)類:

package com.yyq.aspectJAdvanced;public class NaughtyWaiter implements Waiter { public void greetTo(String clientName) { System.out.println('NaughtyWaiter:greet to ' + clientName + '...'); } public void serveTo(String clientName) { System.out.println('NaughtyWaiter:serving ' + clientName + '...'); } public void joke(String clientName, int times) { System.out.println('NaughtyWaiter:play ' + times + ' jokes to ' + clientName + '...'); }}

Seller接口:

package com.yyq.aspectJAdvanced;public interface Seller { int sell(String goods, String clientName);}

SmallSeller實(shí)現(xiàn)類:

package com.yyq.aspectJAdvanced;public class SmartSeller implements Seller { public int sell(String goods,String clientName) { System.out.println('SmartSeller: sell '+goods +' to '+clientName+'...'); return 100; } public void checkBill(int billId){ if(billId == 1) throw new IllegalArgumentException('iae Exception'); else throw new RuntimeException('re Exception'); }}

beans.xml配置文件:

<?xml version='1.0' encoding='UTF-8' ?><beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:aop='http://www.springframework.org/schema/aop' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd'> <aop:aspectj-autoproxy proxy-target- /> <bean /> <bean /> <bean /> <!-- <bean /> <bean /> <bean /> <bean /> <bean /> <bean /> <bean /> <bean /> <bean />--></beans>1、切點(diǎn)符合運(yùn)算

使用切點(diǎn)符合運(yùn)算符,我們將擁有強(qiáng)大而靈活的切點(diǎn)表達(dá)能力。

TestAspect:切點(diǎn)符合運(yùn)算定義切面

package com.yyq.aspectJAdvanced;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspectpublic class TestAspect { //與非運(yùn)算 @Before('!target(com.yyq.aspectJAdvanced.NaiveWaiter) && execution(* serveTo(..))') public void notServeInNaiveWaiter(){ System.out.println('--notServeInNaiveWaiter() executed!--'); } //與運(yùn)算 @After('within(com.yyq.aspectJAdvanced.*) && execution(* greetTo(..))') public void greetToFun(){ System.out.println('--greetToFun() executed!--'); } //或運(yùn)算 @AfterReturning('target(com.yyq.aspectJAdvanced.Waiter) || target(com.yyq.aspectJAdvanced.Seller)') public void waiterOrSeller(){ System.out.println('--waiterOrSeller() executed!--'); }}

測(cè)試方法:

@Test public void pointAspectJTest() { String configPath = 'comyyqaspectJAdvancedbeans.xml'; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); Waiter naiveWaiter = (Waiter) ctx.getBean('naiveWaiter'); Waiter naughtyWaiter = (Waiter) ctx.getBean('naughtyWaiter'); naiveWaiter.greetTo('John'); naiveWaiter.serveTo('John'); naughtyWaiter.greetTo('Tom'); naughtyWaiter.serveTo('Tom'); }

輸出結(jié)果:

NaiveWaiter:greet to John...--greetToFun() executed!----waiterOrSeller() executed!--NaiveWaiter:serving to John...--waiterOrSeller() executed!--NaughtyWaiter:greet to Tom...--greetToFun() executed!----waiterOrSeller() executed!----notServeInNaiveWaiter() executed!--NaughtyWaiter:serving Tom...--waiterOrSeller() executed!--2、命名切點(diǎn)

切點(diǎn)直接聲明在增強(qiáng)方法處被稱為匿名切點(diǎn),匿名切點(diǎn)只能在聲明處使用。如果希望在其他地方重用一個(gè)切點(diǎn),我們可以通過@Pointcut注解以及切面類方法對(duì)切點(diǎn)進(jìn)行命名。

TestNamePointcut:命名切點(diǎn)類

package com.yyq.aspectJAdvanced;import org.aspectj.lang.annotation.Pointcut;public class TestNamePointcut { //通過注解方法inPackage()對(duì)該切點(diǎn)進(jìn)行命名,方法可視域修飾符為private,表明該命名切點(diǎn)只能在本切面類中使用 @Pointcut('within(com.yyq.aspectJAdvaned.*)') private void inPackage(){} @Pointcut('execution(* greetTo(..))') protected void greetTo(){} @Pointcut('inPackage() and greetTo()') public void inPkgGreetTo(){}}

TestAspect2:切面實(shí)現(xiàn)類

package com.yyq.aspectJAdvanced;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspectpublic class TestAspect2 { @Before('TestNamePointcut.inPkgGreetTo()') public void pkgGreetTo(){ System.out.println('--pkgGreetTo() executed!--'); } @Before('target(com.yyq.aspectJAdvanced.NaiveWaiter) || TestNamePointcut.inPkgGreetTo()') public void pkgGreetToNotnaiveWaiter(){ System.out.println('--pkgGreetToNotnaiveWaiter() executed!--'); }}

測(cè)試方法:

@Test public void pointAspectJTest2() { String configPath = 'comyyqaspectJAdvancedbeans.xml'; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); NaiveWaiter naiveWaiter = (NaiveWaiter) ctx.getBean('naiveWaiter'); naiveWaiter.smile('Andy', 2); }

輸出結(jié)果:

--pkgGreetToNotnaiveWaiter() executed!--NaiveWaiter:smile to Andy2times...3、增強(qiáng)織入的順序

一個(gè)連接點(diǎn)可以同時(shí)匹配多個(gè)切點(diǎn),切點(diǎn)對(duì)應(yīng)的增強(qiáng)在連接點(diǎn)上的織入順序的安排主要有以下3種情況:

1)如果增強(qiáng)在同一個(gè)切面類中聲明,則依照增強(qiáng)在切面類中定義的順序進(jìn)行織入;

2)如何增強(qiáng)位于不同的切面類中,且這些切面類都實(shí)現(xiàn)了org.springframework.core.Order接口,則由接口方法的順序號(hào)決定(順序號(hào)小的先織入);

3)如果增強(qiáng)位于不同的切面類中,且這些切面類沒有實(shí)現(xiàn)org.springframework.core.Order接口,織入的順序是不確定的。

4、訪問連接點(diǎn)信息

AspectJ使用org.aspectj.lang.JoinPoint接口表示目標(biāo)類連接點(diǎn)對(duì)象,如果是環(huán)繞增強(qiáng)時(shí),使用org.aspectj.lang.ProceedingJoinPoint表示連接點(diǎn)對(duì)象,該類是JoinPoint的子接口,任何一個(gè)增強(qiáng)方法都可以通過將第一個(gè)入?yún)⒙暶鳛镴oinPoint訪問到連接點(diǎn)上下文的信息。

TestAspect3:切面實(shí)現(xiàn)類

@Aspectpublic class TestAspect3 { @Around('execution(* greetTo(..)) && target(com.yyq.aspectJAdvanced.NaiveWaiter)') public void joinPointAccess(ProceedingJoinPoint pjp) throws Throwable { System.out.println('---joinPointAccess---'); System.out.println('args[0]:' + pjp.getArgs()[0]); System.out.println('signature:' + pjp.getTarget().getClass()); pjp.proceed(); System.out.println('---joinPointAccess---'); }}

測(cè)試方法:

@Test public void pointAspectJTest3() { String configPath = 'comyyqaspectJAdvancedbeans.xml'; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); Waiter naiveWaiter = (Waiter) ctx.getBean('naiveWaiter'); naiveWaiter.greetTo('Andy'); }

輸出結(jié)果:

---joinPointAccess---args[0]:Andysignature:class com.yyq.aspectJAdvanced.NaiveWaiterNaiveWaiter:greet to Andy...---joinPointAccess---5、綁定連接點(diǎn)方法入?yún)?p>args()用于綁定連接點(diǎn)方法的入?yún)ⅲ籃annotation()用于綁定連接點(diǎn)方法的注解對(duì)象;而@args()用于綁定連接點(diǎn)方法入?yún)⒌淖⒔狻?/p>

TestAspect4:切面實(shí)現(xiàn)類

@Aspectpublic class TestAspect4 { @Before('target(com.yyq.aspectJAdvanced.NaiveWaiter) && args(name,num,..)') public void bindJoinPointParams(int num, String name) { System.out.println('---bindJoinPointParams---'); System.out.println('name:' + name); System.out.println('num:' + num); System.out.println('---bindJoinPointParams---'); }}

測(cè)試方法:

@Test public void pointAspectJTest4() { String configPath = 'comyyqaspectJAdvancedbeans.xml'; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); NaiveWaiter naiveWaiter = (NaiveWaiter) ctx.getBean('naiveWaiter'); naiveWaiter.smile('Andy', 3); }

輸出結(jié)果:

---bindJoinPointParams---name:Andynum:3---bindJoinPointParams---NaiveWaiter:smile to Andy 3 times...6、綁定代理對(duì)象

使用this()或target()可綁定被代理對(duì)象實(shí)例,在通過類實(shí)例名綁定對(duì)象時(shí),還依然具有原來連接點(diǎn)匹配的功能,只不過類名是通過增強(qiáng)方法中同名入?yún)⒌念愋烷g接決定罷了。

TestAspect5:切面實(shí)現(xiàn)類

@Aspectpublic class TestAspect5 { @Before('this(waiter)') public void bindProxyObj(Waiter waiter){ System.out.println('---bindProxyObj---'); System.out.println(waiter.getClass().getName()); System.out.println('---bindProxyObj---'); }}

測(cè)試方法:

@Test public void pointAspectJTest5() { String configPath = 'comyyqaspectJAdvancedbeans.xml'; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); Waiter waiter = (Waiter) ctx.getBean('naiveWaiter'); waiter.greetTo('Yang'); }

輸出結(jié)果:

---bindProxyObj---com.yyq.aspectJAdvanced.NaiveWaiter$$EnhancerByCGLIB$$fefafe52---bindProxyObj---NaiveWaiter:greet to Yang...7、綁定類注解對(duì)象

@within()和@target()函數(shù)可以將目標(biāo)類的注解對(duì)象綁定到增強(qiáng)方法中,我們通過@within()演示注解綁定的操作。

TestAspect6:切面測(cè)試類

@Aspectpublic class TestAspect6 { @Before('@within(m)') public void bindTypeAnnoObject(Monitorable m) { System.out.println('---bindTypeAnnoObject---'); System.out.println(m.getClass().getName()); System.out.println('---bindTypeAnnoObject---'); }}

測(cè)試方法:

@Test public void pointAspectJTest6() { String configPath = 'comyyqaspectJAdvancedbeans.xml'; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); Waiter waiter = (Waiter) ctx.getBean('naiveWaiter2'); ((NaiveWaiter2)waiter).greetTo('Yang'); }

輸出結(jié)果:

---bindTypeAnnoObject---$Proxy4---bindTypeAnnoObject---NaiveWaiter:greet to Yang...8、綁定返回值

在后置增強(qiáng)中,我們可以通過returning綁定連接點(diǎn)方法的返回值。

TestAspect7:切面實(shí)現(xiàn)類

@Aspectpublic class TestAspect7 { @AfterReturning(value = 'target(com.yyq.aspectJAdvanced.SmartSeller)', returning = 'retVal') public void bindReturnValue(int retVal) { System.out.println('---bindReturnValue---'); System.out.println('returnValue:' + retVal); System.out.println('---bindReturnValue---'); }}

測(cè)試方法:

@Test public void pointAspectJTest7() { String configPath = 'comyyqaspectJAdvancedbeans.xml'; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); SmartSeller seller = (SmartSeller) ctx.getBean('seller'); seller.sell('Beer', 'John'); }

輸出結(jié)果:

SmartSeller: sell Beer to John...---bindReturnValue---returnValue:100---bindReturnValue---9、綁定拋出的異常

和通過切點(diǎn)函數(shù)綁定連接點(diǎn)信息不同,連接點(diǎn)拋出的異常必須使用AfterThrowing注解的throwing成員進(jìn)行綁定。

TestAspect8:切面實(shí)現(xiàn)類

@Aspectpublic class TestAspect8 { @AfterThrowing(value = 'target(com.yyq.aspectJAdvanced.SmartSeller)', throwing = 'iae') public void bindException(IllegalArgumentException iae) { System.out.println('---bindException---'); System.out.println('exception:' + iae.getMessage()); System.out.println('---bindException---'); }}

測(cè)試方法:

@Test public void pointAspectJTest8() { String configPath = 'comyyqaspectJAdvancedbeans.xml'; ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); SmartSeller seller = (SmartSeller) ctx.getBean('seller'); seller.checkBill(1); }

輸出結(jié)果:

---bindException---exception:iae Exception---bindException---

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 久久久精品久久视频只有精品 | 成人伊人 | 午夜香蕉成视频人网站高清版 | 高清成人爽a毛片免费网站 高清大学生毛片一级 | 手机免费黄色网址 | 欧美三级欧美一级 | 国产综合精品在线 | 欧美三级黄色大片 | 亚洲国产品综合人成综合网站 | 欧美成人亚洲综合精品欧美激情 | 亚洲美女在线观看亚洲美女 | 亚洲国产cao | 国产成人精品无缓存在线播放 | 特级毛片免费观看视频 | 日本黄页网站免费大全 | 久久18| 99精品国产免费久久国语 | 国产极品精频在线观看 | 久久综合久久精品 | 精品亚洲视频在线 | 久草在线免费资源站 | 亚洲最新 | 欧美三级成人观看 | 精品久久网站 | 久久精品视频免费在线观看 | 国产三级小视频在线观看 | 91av综合 | 新婚第一次一级毛片 | 国产永久高清免费动作片www | 成人做爰视频www | 各种偷拍盗摄视频在线观看 | 国产在线视频区 | 亚洲国产精品一区二区不卡 | 日本特爽特黄特刺激大片 | 一区二区三区亚洲视频 | 玖玖色视频 | 免费毛片全部不收费的 | 一本色道久久88加勒比—综合 | 日韩aⅴ在线观看 | 日本亲子乱子伦视频 | 欧美激情中文字幕 |