spring3: 切面及通知实例 Aspectj的aop
生活随笔
收集整理的這篇文章主要介紹了
spring3: 切面及通知实例 Aspectj的aop
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.前置通知
接口:
package chapter1.server;public interface IHelloService {public void sayAdvisorBefore(String param) ; }實現(xiàn)
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); }實現(xiàn)
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); }實現(xiàn):
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.環(huán)繞通知
接口:
package chapter1.server;public interface IHelloService4 {public void sayAround(String param); }實現(xiàn):
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;} }測試程序:
//環(huán)繞通知 public void testAround(){ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj4.xml");IHelloService4 hello = context.getBean("helloService", IHelloService4.class);hello.sayAround("gaga ya xia ba");}
?
?
5.引入(結(jié)合chatper1.service.IHelloService程序來試驗)
接口:
package chapter1.server;public interface IHelloService5 {public void sayDeclare(); }實現(xiàn):
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();}
?
轉(zhuǎn)載于:https://www.cnblogs.com/achengmu/p/8580207.html
總結(jié)
以上是生活随笔為你收集整理的spring3: 切面及通知实例 Aspectj的aop的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python on the Way, D
- 下一篇: 齐次常系数递推关系式