在Spring 2.0中,除了傳統的通過實現AOP AIP的方式來實現Advice之外,還提供了兩種更加簡便的方式來實現Advice:1)基于XML Schema的設置;2)基于Annotation的支持,采用這兩種方式,Advice將不用實現特定的接口。現在讓我們來看看如何使用這兩種方式來分別實現Before Advice、After Advice、Around Advice、Throwing Advice。
    一、Before Advice:基于XML Schema
當基于XML Schema實現Before Advice時,你的Advice類不用實現org.springframework.aop.MethodBeforeAdvice接口,例如:
java 代碼
 

   
1package com.savage.aop;  
   
2.   
   
3import org.aspectj.lang.JoinPoint;  
   
4.   
   
5public class LogBeforeAdvice {  
   
6.     public void before(JoinPoint joinPoint) {  
   
7.         System.out.println("Logging before " + joinPoint.getSignature().getName());  
   
8.     }  
   
9. }  



          before方法是在目標對象上的方法被執行前要執行的方法,before方法中的JoinPoint參數是可選項,你可以根據需要決定是否需要JoinPoint參數,通過JoinPoint對象,你可以獲得目標對象(getTarget())、目標方法上的參數(getArgs())等信息。
     然后在XML中為目標對象指定LogBeforeAdvice代理:
xml 代碼
 

   
1<?xml version="1.0" encoding="UTF-8"?>  
   
2<beans xmlns="http://www.springframework.org/schema/beans"  
   
3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   
4.     xmlns:aop="http://www.springframework.org/schema/aop"  
   
5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
   6.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
   7.     http://www.springframework.org/schema/aop  
   8.     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
   9.     <bean id="messageSender" class="com.savage.aop.HttpMessageSender"></bean>  
  
10.       
  
11.     <bean id="logBeforeAdvice" class="com.savage.aop.LogBeforeAdvice"></bean>  
  
12.       
  
13.     <aop:config>  
  
14.         <aop:aspect id="logBefore" ref="logBeforeAdvice">  
  
15.             <aop:before pointcut="execution(* com.savage.aop.MessageSender.*(..))"
  
16.                         method="before"/>  
  
17.         </aop:aspect>  
  
18.     </aop:config>  
  
19</beans>  



     如上所示,在Spring 
2.0中要使用基于XML Sechma聲明AOP的方式,需要在XML中加入aop的名稱空間。當基于XML Sechma實現AOP時,所有的AOP都是在<aop:config></aop:config>標簽中聲明的,<aop:aspect></aop:aspect>用于定義Advice實例。<aop:before></aop:before>表示當前實例用于實現Before Advice;pointcut屬性用于指定pointcut表示式,上面的例子表示此Advice將應用于com.savage.aop.MessageSender接口中的任何方法;method屬性表示Advice上要調用的方法。
     現在調用任何MessageSender接口上的方法之前都會執行LogBeforeAdvice的before方法,例如:
java 代碼
 

   
1package com.savage.aop;  
   
2.   
   
3import org.springframework.context.ApplicationContext;  
   
4import org.springframework.context.support.ClassPathXmlApplicationContext;  
   
5.   
   
6public class AdviceDemo {  
   
7.     public static void main(String[] args) {  
   
8.         ApplicationContext context =
   
9.                            new ClassPathXmlApplicationContext("beans-config.xml");  
  
10.         MessageSender sender = (MessageSender)context.getBean("messageSender");  
  
11.         sender.sendMessage("message");  
  
12.     }  
  
13. }  


     二、Before Advice:基于Annotation
         使用Annotation來實現Advice,在XML文件上的定義要比基于XML Sechema的方法要簡便的多,但在實現Before Advice類時,則需要使用到@Aspect、@Before標識,并需要引入org.aspectj.lang.annotation包中的類。還以LogBeforeAdvice為例,LogBeforeAdvice類需要改為:
java 代碼
 

   
1package com.savage.aop;  
   
2.   
   
3import org.aspectj.lang.JoinPoint;  
   
4import org.aspectj.lang.annotation.Aspect;  
   
5import org.aspectj.lang.annotation.Before;  
   
6.   
   
7. @Aspect  
   
8public class LogBeforeAdvice {  
   
9.     @Before("execution(* com.savage.aop.MessageSender.*(..))")  
  
10.     public void before(JoinPoint joinPoint) {  
  
11.         System.out.println("Logging before " + joinPoint.getSignature().getName());  
  
12.     }  
  
13. }  



     如上所示,通過@Aspect將一個類聲明為Aspect類,通過@Before將方法聲明Before Advice,方法中的JoinPoint同樣是可選的。然后在XML文件中做如下定義:
xml 代碼
 

   
1<?xml version="1.0" encoding="UTF-8"?>  
   
2<beans xmlns="http://www.springframework.org/schema/beans"  
   
3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   
4.     xmlns:aop="http://www.springframework.org/schema/aop"  
   
5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
   6.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
   7.     http://www.springframework.org/schema/aop  
   8.     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
   9.     <bean id="messageSender" class="com.savage.aop.HttpMessageSender"></bean>  
  
10.       
  
11.     <bean id="logBeforeAdvice" class="com.savage.aop.LogBeforeAdvice"></bean>  
  
12.       
  
13.     <aop:aspectj-autoproxy/>  
  
14</beans>  



     所有基于Annotation實現的Advice,在XML文件中都只要使用
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>進行設置就可以了,非常簡單。

     三、After Advice:基于XML Sechma
         和Before Advice一樣,基于XML Sechma實現After Returning Advice時,不再需要org.springframework.aop.AfterReturningAdvice接口:
java 代碼
 

   
1package com.savage.aop;  
   
2.   
   
3import org.aspectj.lang.JoinPoint;  
   
4.   
   
5public class LogAfterReturningAdvice {  
   
6.     public void afterReturning(JoinPoint joinPoint) {  
   
7.         System.out.println("Logging after " + joinPoint.getSignature().getName());  
   
8.     }  
   
9. }  



然后在XML中做如下設置:
xml 代碼
 

   
1<?xml version="1.0" encoding="UTF-8"?>  
   
2<beans xmlns="http://www.springframework.org/schema/beans"  
   
3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   
4.     xmlns:aop="http://www.springframework.org/schema/aop"  
   
5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
   6.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
   7.     http://www.springframework.org/schema/aop  
   8.     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
   9.     <bean id="messageSender" class="com.savage.aop.HttpMessageSender"></bean>  
  
10.       
  
11.     <bean id="logAfterReturningAdvice" 
  
12.           class="com.savage.aop.LogAfterReturningAdvice"></bean>  
  
13.       
  
14.     <aop:config>  
  
15.         <aop:aspect id="logAfterReturning" ref="logAfterReturningAdvice">  
  
16.             <aop:after-returning   
  
17.                 pointcut="execution(* com.savage.aop.MessageSender.*(..))"   
  
18.                 method="logAfterReturning"/>  
  
19.         </aop:aspect>  
  
20.     </aop:config>  
  
21</beans>  



    四、After Advice:基于Annotation
        和Before Advice相似,使用@AfterReturning來表示After Returning Advice:
java 代碼
 

   
1package com.savage.aop;  
   
2.   
   
3import org.aspectj.lang.JoinPoint;  
   
4import org.aspectj.lang.annotation.Aspect;  
   
5import org.aspectj.lang.annotation.AfterReturning;  
   
6.   
   
7. @Aspect  
   
8public class AfterReturningAdvice {  
   
9.     @AfterReturning(pointcut="execution(* com.savage.aop.MessageSender.*(..))",
  
10.                     returning="retVal")  
  
11.     public void afterReturning(JoinPoint joinPoint, Object retVal) {  
  
12.         System.out.println("Logging after " + joinPoint.getSignature().getName());  
  
13.     }  
  
14. }  



     這里和Before Advice有點不同的是,在定義Poincut表示式時,多了一個returning屬性,用于指定目標方法執行完后的返回值。
     XML文件中的設置與LogBeforeAdvice的相似(將logBeforeAdvice的定義改為logAfterReturning的定義),不再列舉。

     五、Around Advice:基于XML Sechma
          在Spring 
2.0中,Around Advice不用實現org.aoplliance.intercept.MethodInterceptor接口,但Advice的方法必須返回對象,并且必須定義一個ProceedingJoinPoint參數,例如:
java 代碼
 

   
1package com.savage.aop;  
   
2.   
   
3import org.aspectj.lang.ProceedingJoinPoint;  
   
4.   
   
5public class LogAroundAdvice {  
   
6.     public void invoke(ProceedingJoinPoint joinPoint) {  
   
7.         System.out.println("Logging before " + joinPoint.getSignature().getName());  
   
8.         Object retVal = joinPoint.proceed();  
   
9.         System.out.println("Logging after " + joinPoint.getSignature().getName());  
  
10.         return retVal;  
  
11.     }  
  
12. }  



XML中的設置如下:
xml 代碼
 

   
1<?xml version="1.0" encoding="UTF-8"?>  
   
2<beans xmlns="http://www.springframework.org/schema/beans"  
   
3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   
4.     xmlns:aop="http://www.springframework.org/schema/aop"  
   
5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
   6.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
   7.     http://www.springframework.org/schema/aop  
   8.     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
   9.     <bean id="messageSender" class="com.savage.aop.HttpMessageSender"></bean>  
  
10.       
  
11.     <bean id="logAroundAdvice" class="com.savage.aop.LogAroundAdvice"></bean>  
  
12.       
  
13.     <aop:config>  
  
14.         <aop:aspect id="logAround" ref="logAroundAdvice">  
  
15.             <aop:around   
  
16.                 pointcut="execution(* com.savage.aop.MessageSender.*(..))"   
  
17.                 method="invoke"/>  
  
18.         </aop:aspect>  
  
19.     </aop:config>  
  
20</beans>  



     六、Around Advice:基于Annotation
          和Before Advice相似,使用@Around來表示Around Advice:
java 代碼
 

   
1package com.savage.aop;  
   
2.   
   
3import org.aspectj.lang.ProceedingJoinPoint;  
   
4import org.aspectj.lang.annotation.Aspect;  
   
5import org.aspectj.lang.annotation.Around;  
   
6.   
   
7. @Aspect  
   
8public class AfterReturningAdvice {  
   
9.     @Around("execution(* com.savage.aop.MessageSender.*(..))")  
  
10.     public void invoke(ProceedingJoinPoint joinPoint) {  
  
11.         System.out.println("Logging before " + joinPoint.getSignature().getName());  
  
12.         Object retVal = joinPoint.proceed();  
  
13.         System.out.println("Logging after " + joinPoint.getSignature().getName());  
  
14.         return retVal;  
  
15.     }  
  
16. }  



     XML文件中的設置與LogBeforeAdvice的相似(將logBeforeAdvice的定義改為logAroundAdvice的定義),不再列舉。

     七、Throw Advice:基于XML Sechma
         在Spring 
2.0中,Throw Advice不用實現org.springframework.aop.ThrowsAdvice接口,但Advice的方法必須定義Throwable(或其子類)參數,例如:
java 代碼
 

   
1package com.savage.aop;  
   
2.   
   
3import org.aspectj.lang.JoinPoint;  
   
4.   
   
5public class LogThrowingAdvice {  
   
6.     public void afterThrowing (JoinPoint joinPoint, Throwable throwable) {  
   
7.         System.out.println("Logging when throwing " + joinPoint.getSignature().getName());  
   
8.     }  
   
9. }  



在XML的設置如下:
xml 代碼
 

   
1<?xml version="1.0" encoding="UTF-8"?>  
   
2<beans xmlns="http://www.springframework.org/schema/beans"  
   
3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   
4.     xmlns:aop="http://www.springframework.org/schema/aop"  
   
5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
   6.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
   7.     http://www.springframework.org/schema/aop  
   8.     http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
   9.     <bean id="messageSender" class="com.savage.aop.HttpMessageSender"></bean>  
  
10.       
  
11.     <bean id="logThrowingAdvice" class="com.savage.aop.LogThrowingAdvice"></bean>  
  
12.       
  
13.     <aop:config>  
  
14.         <aop:aspect id="logThrowing" ref="logThrowingAdvice">  
  
15.             <aop:after-throwing   
  
16.                 pointcut="execution(* com.savage.aop.MessageSender.*(..))"  
  
17.                 throwing="throwable"  
  
18.                 method="afterThrowing"/>  
  
19.         </aop:aspect>  
  
20.     </aop:config>  
  
21</beans>  



     在
<aop:after-throwing></aop:after-throwing>中必須定義throwing屬性,指定方法中的throwable參數。Spring將根據異常類型決定是否調用afterThrowing方法。

     八、Throw Advice:基于Annotation
java 代碼
 

   
1package com.savage.aop;  
   
2.   
   
3import org.aspectj.lang.JoinPoint;  
   
4import org.aspectj.lang.annotation.Aspect;  
   
5import org.aspectj.lang.annotation.AfterThrowing;  
   
6.   
   
7. @Aspect  
   
8public class AfterThrowingAdvice {  
   
9.     @AfterThrowing(pointcut="execution(* com.savage.aop.MessageSender.*(..))",
  
10.                    throwing="throwable")  
  
11.     public void afterThrowing(JoinPoint joinPoint, Throwable throwable) {  
  
12.         System.out.println("Logging when throwing "
  
13.                            + joinPoint.getSignature().getName());  
  
14.     }  
  
15. }  



       XML文件中的設置與LogBeforeAdvice的相似(將logBeforeAdvice的定義改為logThrowingAdvice的定義),不再列舉。

    九、Pointcut
    在Spring 
2.0中,