四種類型的通知(Advice)
下面來介紹 4 種類型的通知(Advice)。
Before Advice
method 執行前,將執行下面的程式碼。
HijackBeforeMethod.java 如下:
package com.shiyanlou.spring.aop.advice; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class HijackBeforeMethod implements MethodBeforeAdvice { public void before(Method arg0, Object[] args, Object target) throws Throwable { System.out.println("HijackBeforeMethod : Before method hijacked!"); } }
在配置檔案中加入新的 bean 配置 HijackBeforeMethod,然後建立一個新的代理(proxy),命名為 customerServiceProxy。target 定義你想劫持哪個 bean;interceptorNames 定義想用哪個 class(advice) 劫持 target。ApringAOPAdvice.xml 如下:
<beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "customerService" class = "com.shiyanlou.spring.aop.advice.CustomerService"> <property name = "name" value = "Shiyanlou" /> <property name = "url" value = "https://www.lanqiao.cn" /> </bean> <bean id = "hijackBeforeMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackBeforeMethod" /> <bean id = "customerServiceProxy" class = "org.springframework.aop.framework.ProxyFactoryBean"> <property name = "target" ref = "customerService" /> <property name = "interceptorNames"> <list> <value>hijackBeforeMethodBean</value> </list> </property> </bean> </beans>
用 Spring proxy 之前,必須新增 CGLIB 類庫,在之前的 pom.xml 檔案中,已經新增到了其中,以下是 pom.xml 依賴:
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.9</version>
</dependency>
App.java 如下:
package com.shiyanlou.spring.aop.advice; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext appContext = new ClassPathXmlApplicationContext( new String[] { "SpringAOPAdvice.xml" }); CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy"); System.out.println("使用Spring AOP 如下"); System.out.println("*************************"); cust.printName(); System.out.println("*************************"); cust.printURL(); System.out.println("*************************"); try { cust.printThrowException(); } catch (Exception e) { } } }
輸入命令:
mvn compile
mvn exec:java -Dexec.mainClass="com.shiyanlou.spring.aop.advice.App"
實驗結果如下:
每一個 customerService 的 method 執行前,都將先執行 HijackBeforeMethod 的 before 方法。
After Returning Advice
建立一個實現了介面 AfterReturningAdvice 的 class,method 執行後,直到返回結果後,才執行下邊的程式碼,如果沒有返回結果,將不執行切入的程式碼。
HijackAfterMethod.java 如下:
package com.shiyanlou.spring.aop.advice;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class HijackAfterMethod implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("HijackAfterMethod : After method hijacked!");
}
}
修改 bean 配置檔案,加入 hijackAfterMethodBean 配置,ApringAOPAdvice.xml 如下:
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "customerService" class = "com.shiyanlou.spring.aop.advice.CustomerService">
<property name = "name" value = "lanqiao" />
<property name = "url" value = "lanqiao.cn" />
</bean>
<bean id = "hijackBeforeMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackBeforeMethod" />
<bean id = "hijackAfterMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackAfterMethod" />
<bean id = "customerServiceProxy" class = "org.springframework.aop.framework.ProxyFactoryBean">
<property name = "target" ref = "customerService" />
<property name = "interceptorNames">
<list>
<value>hijackAfterMethodBean</value>
</list>
</property>
</bean>
</beans>
現在再執行 App.java 後輸出如下:
每一個 customerService 的 method 執行後,都將先執行 HijackAfterMethod 的 afterReturning 方法。
Afetr Throwing Advice
建立一個實現了 ThrowsAdvice 介面的 class,劫持 IllegalArgumentException 異常,目標 method 執行時,丟擲 IllegalArgumentException 異常後,執行切入的方法。HijackThrowExceptionMethod.java 如下:
package com.shiyanlou.spring.aop.advice;
import org.springframework.aop.ThrowsAdvice;
public class HijackThrowExceptionMethod implements ThrowsAdvice {
public void afterThrowing(IllegalArgumentException e) throws Throwable {
System.out.println("HijackThrowException : Throw exception hijacked!");
}
}
修改 bean 配置檔案,加入了 hijackThrowExceptionBean,ApringAOPAdvice.xml 如下:
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "customerService" class = "com.shiyanlou.spring.aop.advice.CustomerService">
<property name = "name" value = "lanqiao" />
<property name = "url" value = "lanqiao.cn" />
</bean>
<bean id = "hijackBeforeMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackBeforeMethod" />
<bean id = "hijackAfterMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackAfterMethod" />
<bean id = "hijackThrowExceptionBean" class = "com.shiyanlou.spring.aop.advice.HijackThrowExceptionMethod" />
<bean id = "customerServiceProxy" class = "org.springframework.aop.framework.ProxyFactoryBean">
<property name = "target" ref = "customerService" />
<property name = "interceptorNames">
<list>
<value>hijackThrowExceptionBean</value>
</list>
</property>
</bean>
</beans>
執行結果如下:
Around Advice
結合了以上 3 種形式的 Advice,建立一個實現了介面 MethodInterceptor 的 class,你必須通過 methodInvocation.proceed() 來呼叫原來的方法,即通過呼叫 methodInvocation.proceed() 來呼叫 CustomerService 中的每一個方法,當然也可以不呼叫原方法 HijackAroundMethod.java 如下:
package com.shiyanlou.spring.aop.advice;
import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class HijackAroundMethod implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("Method name : "
+ methodInvocation.getMethod().getName());
System.out.println("Method arguments : "
+ Arrays.toString(methodInvocation.getArguments()));
// 相當於 MethodBeforeAdvice
System.out.println("HijackAroundMethod : Before method hijacked!");
try {
// 呼叫原方法,即呼叫 CustomerService 中的方法
Object result = methodInvocation.proceed();
// 相當於 AfterReturningAdvice
System.out.println("HijackAroundMethod : After method hijacked!");
return result;
} catch (IllegalArgumentException e) {
// 相當於 ThrowsAdvice
System.out.println("HijackAroundMethod : Throw exception hijacked!");
throw e;
}
}
}
修改 bean 配置檔案,加入了 hijackAroundMethodBean,ApringAOPAdvice.xml 如下:
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "customerService" class = "com.shiyanlou.spring.aop.advice.CustomerService">
<property name = "name" value = "lanqiao" />
<property name = "url" value = "lanqiao.cn" />
</bean>
<bean id = "hijackBeforeMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackBeforeMethod" />
<bean id = "hijackAfterMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackAfterMethod" />
<bean id = "hijackThrowExceptionBean" class = "com.shiyanlou.spring.aop.advice.HijackThrowExceptionMethod" />
<bean id = "hijackAroundMethodBean" class = "com.shiyanlou.spring.aop.advice.HijackAroundMethod" />
<bean id = "customerServiceProxy" class = "org.springframework.aop.framework.ProxyFactoryBean">
<property name = "target" ref = "customerService" />
<property name = "interceptorNames">
<list>
<value>hijackAroundMethodBean</value>
</list>
</property>
</bean>
</beans>
執行結果: