日韩性视频-久久久蜜桃-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的全部內容,希望文章能夠幫你解決所遇到的問題。

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