日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring3: 切面及通知实例 Aspectj的aop

發布時間:2025/3/15 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring3: 切面及通知实例 Aspectj的aop 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.前置通知

接口:

package chapter1.server;public interface IHelloService {public void sayAdvisorBefore(String param) ; }

  實現

package chapter1.service.impl;import chapter1.server.IHelloService;public class HelloService implements IHelloService { public void sayAdvisorBefore(String param) {// TODO Auto-generated method stubSystem.out.println("============say " + param);}}

  配置:

<!-- 啟動對Aspectj的支持 --> <aop:aspectj-autoproxy/> <bean id="helloService" class="chapter1.service.impl.HelloService" /> <bean id="aspect" class="chapter1.aop.HelloAspect"/>

  aop:

package chapter1.aop;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut;@Aspect public class HelloAspect {//定義切入點@Pointcut(value="execution(* chapter1..*.sayAdvisorBefore(java.lang.String)) && args(param)", argNames = "param")public void beforePointcut(String param) {}//定義通知@Before(value = "beforePointcut(param)", argNames = "param")public void beforeAdvice(String param) {System.out.println("===========before advice param:" + param);} }

  測試程序:

//前置通知public void testAspectj(){ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj.xml");IHelloService hello = context.getBean("helloService", IHelloService.class);hello.sayAdvisorBefore("before");}

  

?

2.后置返回通知

接口

package chapter1.server;public interface IHelloService2 {public int sayAfterReturning(String param); }

  實現

package chapter1.service.impl;import chapter1.server.IHelloService2;public class HelloService2 implements IHelloService2 {public int sayAfterReturning(String param) {// TODO Auto-generated method stubSystem.out.println("============ say after returning:" + param);return 1;}}

  配置:

<aop:aspectj-autoproxy/> <bean id="helloService" class="chapter1.service.impl.HelloService2" /> <bean id="aspect" class="chapter1.aop.HelloAspect2"/>

  aop:

package chapter1.aop;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.AfterReturning;@Aspect public class HelloAspect2 {//方法一//通知@AfterReturning( //value="execution(* chapter1..*.sayAdvisorBefore(java.lang.String)) and args(param)",value="execution(* chapter1..*.sayAfterReturning(..))", argNames="retVal", returning="retVal")public void afterReturningAdvice(Object retVal){System.out.println("================= return after advice : " + retVal);}//方法二//定義切入點@Pointcut(value="execution(* chapter1..*.sayAfterReturning(java.lang.String) and args(param))", argNames="param")public void returnPointcut(String param) {}public void afterReturningAdvice2(Object retVal){}}

  測試程序:

//后置返回通知public void testAspectAfterReturning(){ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj2.xml");IHelloService2 hello = context.getBean("helloService", IHelloService2.class);hello.sayAfterReturning("hahah");}

  

?

?

3.后置錯誤通知

接口

package chapter1.server;public interface IHelloService3 {public boolean sayAfterThrow(String param); }

  實現:

package chapter1.service.impl;import chapter1.server.IHelloService3;public class HelloService3 implements IHelloService3 {public boolean sayAfterThrow(String param) {// TODO Auto-generated method stubSystem.out.println("=========say after throw:" + param);throw new RuntimeException(); }}

  配置:

<!-- 開啟對Aspectj的支持 --> <aop:aspectj-autoproxy /> <bean id="helloService" class="chapter1.service.impl.HelloService3" /> <bean id="aspect" class="chapter1.aop.HelloAspect3" />

  aop:

package chapter1.aop;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.AfterThrowing;@Aspect public class HelloAspect3 {@AfterThrowing(value="execution(* chapter1..*.sayAfterThrow(java.lang.String))", argNames="exception", throwing="exception")public void afterThrowAdvice(Exception exception){System.out.println("=========after throwing advice : " + exception);} }

  測試程序:

//后置錯誤通知 public void testAfterThrow(){ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj3.xml");IHelloService3 hello = context.getBean("helloService", IHelloService3.class);hello.sayAfterThrow("error");}

  

?

?

4.環繞通知

接口:

package chapter1.server;public interface IHelloService4 {public void sayAround(String param); }

  實現:

package chapter1.service.impl;import chapter1.server.IHelloService4;public class HelloService4 implements IHelloService4 {public void sayAround(String param) {// TODO Auto-generated method stubSystem.out.println("============= say around: " + param);}}

  配置:

<!-- 開啟對Aspectj的支持 --> <aop:aspectj-autoproxy/> <bean id="helloService" class="chapter1.service.impl.HelloService4" /> <bean id="aspect" class="chapter1.aop.HelloAspect4"/>

  aop:

package chapter1.aop;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Around;@Aspect public class HelloAspect4 {@Around(value="execution(* chapter1..*.sayAround(java.lang.String))", argNames="param")public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable{System.out.println("========= around before advice");Object retVal = pjp.proceed(new Object[] {"我被替換了呀"});System.out.println("========= around before advice");return retVal;} }

  測試程序:

//環繞通知 public void testAround(){ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj4.xml");IHelloService4 hello = context.getBean("helloService", IHelloService4.class);hello.sayAround("gaga ya xia ba");}

  

?

?

5.引入(結合chatper1.service.IHelloService程序來試驗)

接口:

package chapter1.server;public interface IHelloService5 {public void sayDeclare(); }

  實現:

package chapter1.service.impl;import chapter1.server.IHelloService5;public class HelloService5 implements IHelloService5 {public void sayDeclare() {// TODO Auto-generated method stubSystem.out.println("=========== say declare " ); }}

  配置:

<!-- 開啟對Aspect的支持 --> <aop:aspectj-autoproxy/> <bean id="helloService" class="chapter1.service.impl.HelloService"/> <bean id="aspect" class="chapter1.aop.HelloAspect5"/>

  aop:

package chapter1.aop;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.DeclareParents;import chapter1.server.IHelloService5; import chapter1.service.impl.HelloService5;@Aspect public class HelloAspect5 {@DeclareParents(value="chapter1..*.HelloService+",defaultImpl=HelloService5.class)public IHelloService5 ihelloService5;}

  測試程序:

//引入@Testpublic void testDeclare(){ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj5.xml");IHelloService5 hello = context.getBean("helloService", IHelloService5.class);hello.sayDeclare();}

  

?

轉載于:https://www.cnblogs.com/achengmu/p/8580207.html

總結

以上是生活随笔為你收集整理的spring3: 切面及通知实例 Aspectj的aop的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 香港三日本三级少妇66 | 啪啪官网| 精品午夜久久 | 久草视频在线看 | 97久久人人超碰caoprom欧美 | 午夜精品av | 操欧美老逼 | 欧美顶级metart裸体全部自慰 | 伊人春色网站 | 成人午夜免费观看 | 香蕉综合视频 | 午夜爱爱毛片xxxx视频免费看 | 国产a区 | 黑人巨大精品欧美黑寡妇 | 借种(出轨高h) | 久久久久久国产精品日本 | 精品中文字幕一区 | 中文字幕一区二区人妻痴汉电车 | 色人阁五月天 | 欧美伦理影院 | 在线看不卡av | 国产精品中文久久久久久 | 中文字幕av网站 | 久久91精品国产91久久小草 | 又色又爽又黄无遮挡的免费视频 | 香蕉视频在线观看视频 | 欧美人与按摩师xxxx | 国产美女精品久久 | 成人午夜视频在线播放 | 日韩高清三区 | av卡一卡二 | 草久久久久 | 久草视频免费播放 | 亚洲国产一区二区三区四区 | 亚洲精品成a人在线观看 | 色小说综合| 久热最新 | 理论片大全免费理伦片 | 在线播放av网站 | 日韩r级电影在线观看 | 在线观看成人免费视频 | 亚洲一区中文字幕永久在线 | 国产嘿咻 | 成人区人妻精品一熟女 | 91精品人妻互换一区二区 | 91污网站 | 久久com| 国产91边播边对白在线 | 久久久情 | 欧美性视屏 | 91嫩草欧美久久久九九九 | 久久亚洲精品国产 | 韩日成人 | 亚洲第一成人网站 | 九九色网站| 黄色片网站免费在线观看 | 男女那个视频 | 亚洲性欧美 | 中文字幕无码人妻少妇免费 | 国产又黄又爽又色 | 欧美亚洲在线观看 | www.com黄色| 天天操天天摸天天干 | 被警察猛c猛男男男 | www.夜夜爱 | 韩国三级视频 | 欧美精品片 | 日韩欧美精品在线 | 久久久欧美| 国产av人人夜夜澡人人爽麻豆 | 亚洲精品乱码久久久久久自慰 | 亚洲一区二区三区 | 91精品在线视频观看 | 亚洲综合一区中 | 91欧美在线视频 | yy6080久久 | 日韩一区视频在线 | 免费看a | 99热99这里只有精品 | 久操视频网站 | 日本伦理片在线看 | 国产高清在线免费观看 | 91精品国产一区二区三区蜜臀 | 超碰2025| 一二三四av | 日韩资源在线观看 | 公肉吊粗大爽色翁浪妇视频 | 欧美性猛交xxxx久久久 | 成人午夜在线免费观看 | 欧美一区2区三区4区公司 | 六月丁香av | 午夜精品久久久久久久99老熟妇 | 六月丁香婷婷网 | 日韩制服在线 | 四虎网址大全 | 精品国产91久久久久久久妲己 | 一级大片儿 | 亚洲伦理中文字幕 | 亚洲国产中文在线 |