springframework(九)AOP的advices,中规中矩的使用方式

来源:互联网 发布:淘宝打包 编辑:程序博客网 时间:2024/06/07 00:24

Spring的Advices
Advices实现了Aspect的真正逻辑。由于织入至Targets的实际不同,spring提供了不同的Advices,像Before Advice,After Advice,Around Advice,Throw Advice。
(1)、Before Advice
通过实现MethodBeforeAdvice来定义
(2)、After Advice
通过实现AfterReturningAdvice来定义
(3)、Around Advice
通过实现MethodInterceptor来定义
(4)、Throw Advice
通过实现ThrowsAdvice来定义

 

我们将使用一个代理的bean aaa,给这个代理的bean加上我们spring的advice,看看他的日志输出表现。我们通过代码示例来说明:

1、定义基本的advice Bean以及要被调用的类

(1)、beforeAdvice的Bean

[java] view plain copy
  1. package com.itcast.advice;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5. import org.springframework.aop.MethodBeforeAdvice;  
  6.   
  7. /** 
  8.  * 调用方法之前 
  9.  * */  
  10. public class LogBeforeAdvice implements MethodBeforeAdvice {  
  11.     public void before(Method method, Object[] args, Object target)  
  12.             throws Throwable {  
  13.         System.out.println("beforeAdvice 调用方法之前被执行!...........");  
  14.     }  
  15. }  

(2)、afterAdvice的bean

[java] view plain copy
  1. package com.itcast.advice;  
  2.   
  3. import java.lang.reflect.Method;  
  4. import org.springframework.aop.AfterReturningAdvice;  
  5.   
  6. public class LogAfterAdvice implements AfterReturningAdvice {  
  7.     public void afterReturning(Object returnValue, Method method,  
  8.             Object[] args, Object target) throws Throwable {  
  9.         System.out.println("afterAdvice 调用方法之后被执行!...........");  
  10.     }  
  11. }  

(3)、Around advice的Bean

[java] view plain copy
  1. package com.itcast.advice;  
  2.   
  3. import org.aopalliance.intercept.MethodInterceptor;  
  4. import org.aopalliance.intercept.MethodInvocation;  
  5.   
  6. /** 
  7.  * 环绕通知 
  8.  * */  
  9. public class LogAroundAdvice implements MethodInterceptor {  
  10.   
  11.     public Object invoke(MethodInvocation mi) throws Throwable {  
  12.         Object result=null;  
  13.         System.out.println("around 调用方法之前....");  
  14.           
  15.         result=mi.proceed();  
  16.           
  17.         System.out.println("around 调用方法之后...");  
  18.         return result;  
  19.     }  
  20. }  

(4)、Throw Advice的Bean

[c-sharp] view plain copy
  1. package com.itcast.advice;  
  2.   
  3. import java.lang.reflect.Method;  
  4.   
  5.   
  6. import org.springframework.aop.ThrowsAdvice;  
  7.   
  8. /**抛出异常时候的通知*/  
  9. public class ThrowAdvice implements ThrowsAdvice {  
  10.   
  11.     public void afterThrowing(Method method,Object[] args,Object target,Throwable subclass) {  
  12.         System.out.println("ThrowAdvice 记录异常...........");  
  13.     }  
  14.   
  15. }  

(5)、要被代理的类

[java] view plain copy
  1. package com.itcast.proxy;  
  2.   
  3. public class HelloSpeaker1 implements IHello {  
  4.   
  5.     public void hi() {  
  6.         System.out.println("我在HelloSpeaker1中");  
  7.     }  
  8.     public void hiAAA(String aaa) {  
  9.         System.out.println("我在HelloSpeaker1中---hiAAA["+aaa+"]");  
  10.     }  
  11.   
  12.     public void hiBBB(String bbb) throws Exception {  
  13.         System.out.println("我在HelloSpeaker1中---hiBBB["+bbb+"]");  
  14.         throw new Exception("aaa");  
  15.     }  
  16. }  
  17.   
  18. package com.itcast.proxy;  
  19.   
  20. /** 
  21.  * 代理接口 
  22.  * */  
  23. public interface IHello {  
  24.       
  25.     public void hi();  
  26.       
  27.     public void hiAAA(String aaa);  
  28.       
  29.     public void hiBBB(String bbb) throws Exception;  
  30.   
  31. }  

2、在配置文件中声明我们的bean

 

[xhtml] view plain copy
  1. <!-- 代理 -->  
  2. <bean id="helloSpeaker1" class="com.itcast.proxy.HelloSpeaker1"></bean>  
  3.   
  4. <!-- 声明四种通知类型,其实就是你想加入到其他被代理程序执行逻辑中的代码 -->  
  5. <bean id="beforeAdvice" class="com.itcast.advice.LogBeforeAdvice"/>  
  6. <bean id="afterAdvice" class="com.itcast.advice.LogAfterAdvice"></bean>  
  7. <bean id="aroundAdvice" class="com.itcast.advice.LogAroundAdvice"></bean>  
  8. <bean id="throwAdvice" class="com.itcast.advice.ThrowAdvice"></bean>  
  9. <!-- 基础的通知使用,这里强行给helloSpeaker1加上了advice通知 -->  
  10. <bean id="aaa" class="org.springframework.aop.framework.ProxyFactoryBean">  
  11.   <property name="target" ref="helloSpeaker1"></property>  
  12.   <property name="interceptorNames">  
  13.   <list>  
  14.     <value>aroundAdvice</value>  
  15.     <value>beforeAdvice</value>  
  16.     <value>afterAdvice</value>  
  17.     <value>throwAdvice</value>  
  18.   </list>  
  19.   </property>  
  20. </bean>  

3、在main函数中调用

[java] view plain copy
  1. package com.itcast.advice;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. import com.itcast.proxy.IHello;  
  6.   
  7. public class AdviceMain {  
  8. /**对于给基本的spring bean强行指定一批advice方法的调用展示**/  
  9.     public static void main(String[] args) {  
  10.         ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-config.xml");  
  11.         IHello h = (IHello)ctx.getBean("aaa");  
  12.         h.hiAAA("测试配置aop的adivce");  
  13.           
  14.         System.out.println("进行抛出异常的adivce展示-==================");  
  15.         try {  
  16.             h.hiBBB("测试抛出异常的advice");  
  17.         } catch (Exception e) {  
  18.         }  
  19.     }  
  20.   
  21. }  

4、控制台的显示

[xhtml] view plain copy
  1. around 调用方法之前....  
  2. beforeAdvice 调用方法之前被执行!...........  
  3. 我在HelloSpeaker1中---hiAAA[测试配置aop的adivce]  
  4. afterAdvice 调用方法之后被执行!...........  
  5. around 调用方法之后...  
  6. 进行抛出异常的adivce展示-==================  
  7. 15:05:05,671 DEBUG ThrowsAdviceInterceptor:88 - Found exception handler method: public void com.itcast.advice.ThrowAdvice.afterThrowing(java.lang.reflect.Method,java.lang.Object[],java.lang.Object,java.lang.Throwable)  
  8. around 调用方法之前....  
  9. beforeAdvice 调用方法之前被执行!...........  
  10. 我在HelloSpeaker1中---hiBBB[测试抛出异常的advice]  
  11. 15:05:05,687 DEBUG ThrowsAdviceInterceptor:119 - Found handler for exception of type [java.lang.Throwable]: public void com.itcast.advice.ThrowAdvice.afterThrowing(java.lang.reflect.Method,java.lang.Object[],java.lang.Object,java.lang.Throwable)  
  12. ThrowAdvice 记录异常...........  

上边的日志已经说明了,我们给一个bean定义一个代理的bean,然后指定他的拦截器序列,就能在bean中的方法调用的前后,以及抛出异常后进行一系列我们自己定义的操作。

0 0