aop spring

 

今天重新看了 動態代理模式,又看了一遍Spring  AOP,把我的項目慢慢的添加Spring,把我瞭解的AOP和大家分享一下。


下面的例子參考了別人的代碼

添加Spring3.0  需要的包


 

定義一個接口 HelloInterface

  1. package rw.hello;  
  2.   
  3. public interface HelloInterface {  
  4.     void BeforeHello();  
  5.     void ExecuteHello();  
  6.     void AfterHello();   
  7. }  

實現這個接口的類 Hello

  1. package rw.hello;  
  2.   
  3. public class Hello implements HelloInterface{  
  4.   
  5.     public void BeforeHello() {  
  6.         // TODO Auto-generated method stub   
  7.         System.out.println("----------------Hello 方法執行之前");  
  8.           
  9.     }  
  10.   
  11.     public void ExecuteHello() {  
  12.         // TODO Auto-generated method stub   
  13.         System.out.println("----------------Hello 方法執行中......");  
  14.     }  
  15.   
  16.     public void AfterHello() {  
  17.         // TODO Auto-generated method stub   
  18.         System.out.println("----------------Hello 方法執行之後");  
  19.     }  
  20.   
  21. }  

現在想在 Hello 類的
  1. BeforeHello(),ExecuteHello(),AfterHello()這三個方法執行之前做其它事情,比如權限問題,記錄日誌之類.....反正我今天是添加的用戶權限問題  
  2.   
  3. 同樣有3個類,風別是BeforeHello(),ExecuteHello(),AfterHello()這三個方法調用之前要調用的  
  4. 下面只測試一下After.java 和 Before.java  
  5.   
  6. After.java  
  7.   
  8. package rw.method;  
  9.   
  10. import org.aspectj.lang.JoinPoint;  
  11.   
  12.   
  13. public class After {  
  14.   public void invoke(JoinPoint joinpoint){  
  15.       System.out.println("After 類"+joinpoint.getSignature().getName());  
  16.   }  
  17. }  
  18.   
  19. Before.java  
  20.   
  21. package rw.method;  
  22.   
  23. import org.aspectj.lang.JoinPoint;  
  24.   
  25.   
  26. public class Before {  
  27.   public void invoke(JoinPoint joinpoint){  
  28.       System.out.println("Before 類"+joinpoint.getSignature().getName());  
  29.   }  
  30. }  
最注意的還是applicationContext.xml的配置

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop"  
  6.     xmlns:p="http://www.springframework.org/schema/p"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.                         http://www.springframework.org/schema/aop   
  10.                         http://www.springsource.org/schema/aop/spring-aop-3.0.xsd  
  11.                         http://www.springframework.org/schema/tx  
  12.                             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  
  13.   
  14. <bean id="hello" class="rw.hello.Hello"/>  
  15.   
  16. <bean id="after" class="rw.method.After"/>  
  17. <bean id="before" class="rw.method.Before"/>  
  18. <bean id="execute" class="rw.method.Execute"/>  
  19.   
  20. <aop:config>  
  21. <aop:pointcut expression="execution(* rw.hello.HelloInterface.BeforeHello(..))" id="beforepointcut"/>  
  22. <aop:aspect id="beforeaspect" ref="before">  
  23.   <aop:before method="invoke" pointcut-ref="beforepointcut"/>  
  24. </aop:aspect>  
  25. </aop:config>  
  26.   
  27. <aop:config>  
  28.  <aop:pointcut expression="execution(* rw.hello.HelloInterface.AfterHello(..))" id="afterpointcut"/>  
  29.  <aop:aspect id="afteraspact" ref="after">  
  30.     <aop:after method="invoke" pointcut-ref="afterpointcut"/>  
  31.  </aop:aspect>  
  32. </aop:config>  
  33. </beans>  
排版不是很好

1,在Hello的BeforeHello()方法執行之前 會調用Before類中的invoke(JoinPoint joinpoint)方法

2,在Hello的AfterHello()方法執行之後 會調用After類中的invoke(JoinPoint joinpoint)方法

測試類

  1. package rw.test;  
  2.   
  3.   
  4. import org.junit.Before;  
  5. import org.junit.Test;  
  6. import org.springframework.context.ApplicationContext;  
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  8.   
  9. import rw.hello.HelloInterface;  
  10.   
  11.   
  12. public class AllTest {  
  13.     private ApplicationContext context;  
  14.     @Before  
  15.     public void setUp() throws Exception {  
  16.         context=new ClassPathXmlApplicationContext("applicationContext.xml");  
  17.     }  
  18.      @Test  
  19.     public void TestHelloInterface(){  
  20.         HelloInterface hello=(HelloInterface) context.getBean("hello");  
  21.         hello.BeforeHello();  
  22.         hello.AfterHello();  
  23.     }  
  24. }  

輸出結果

Hello 類中的 void ExecuteHello()方法在applicationContext.xml中沒有加以任何配置

正常輸出



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章