本实例是前面Ioc的例子上的延续。
增加类 MyBeforeAdvice.java
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MyBeforeAdvice implements MethodBeforeAdvice{
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("MyBeforeAdvice:before");
}
}
增加类 MyAfterAdvice.java
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class MyAfterAdvice implements AfterReturningAdvice{
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println("MyAfterAdvice:afterReturning");
}
}
增加类 MyAroundAdvice.java
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyAroundAdvice implements MethodInterceptor{
public Object invoke(MethodInvocation arg0) throws Throwable {
Object result=null;
System.out.println("MyAroundAdvice:before");
result = arg0.proceed();
System.out.println("MyAroundAdvice:after");
return result;
}
}
修改mybeans.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="TImpl" class="springtest.TImpl"></bean>
<bean id="MyBeforeAdvice" class="springtest.MyBeforeAdvice"></bean>
<bean id="MyAfterAdvice" class="springtest.MyAfterAdvice"></bean>
<bean id="MyAroundAdvice" class="springtest.MyAroundAdvice"></bean>
<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>springtest.TInterface</value>
</property>
<property name="interceptorNames">
<list>
<value>MyBeforeAdvice</value>
<value>MyAroundAdvice</value>
<value>MyAfterAdvice</value>
</list>
</property>
<property name="target">
<ref bean="TImpl"/>
</property>
</bean>
</beans>
测试类 TTest.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("springtest/mybeans.xml");
TInterface tinterface = (TInterface) ac.getBean("student");//测试与XML配置文件中代理类的bean的ID一致
TService tservice = new TService();
tservice.printName(tinterface);
System.out.println(tinterface.setName("bbb"));
}
}
运行结果正常显示了增强信息。增强的粒度是类,没有到方法。
另,XML配置文件中的<list>标签的红色部分,随着顺序的不同,显示输出的增强代码的顺序有所不同。详情如下:
第一种:
<value>MyBeforeAdvice</value>
<value>MyAroundAdvice</value>
<value>MyAfterAdvice</value>
输出为:
MyBeforeAdvice:before
MyAroundAdvice:before
=======getName===========
MyAfterAdvice:afterReturning
MyAroundAdvice:after
第二种:
<value>MyAroundAdvice</value>
<value>MyBeforeAdvice</value>
<value>MyAfterAdvice</value>
输出为:
MyAroundAdvice:before
MyBeforeAdvice:before
=======getName===========
MyAfterAdvice:afterReturning
MyAroundAdvice:after
第三种:
<value>MyBeforeAdvice</value>
<value>MyAroundAdvice</value>
<value>MyAfterAdvice</value>
输出为:
MyBeforeAdvice:before
MyAroundAdvice:before
=======getName===========
MyAfterAdvice:afterReturning
MyAroundAdvice:after
第四种:
<value>MyAroundAdvice</value>
<value>MyBeforeAdvice</value>
<value>MyAfterAdvice</value>
输出为:
MyAroundAdvice:before
MyBeforeAdvice:before
=======getName===========
MyAfterAdvice:afterReturning
MyAroundAdvice:after
第五种:
<value>MyAroundAdvice</value>
<value>MyAfterAdvice</value>
<value>MyBeforeAdvice</value>
输出为:
MyAroundAdvice:before
MyBeforeAdvice:before
=======getName===========
MyAfterAdvice:afterReturning
MyAroundAdvice:after
第六种:
<value>MyAfterAdvice</value>
<value>MyAroundAdvice</value>
<value>MyBeforeAdvice</value>
输出为:
MyAroundAdvice:before
MyBeforeAdvice:before
=======getName===========
MyAroundAdvice:after
MyAfterAdvice:afterReturning
执行顺序之我见:采用栈(后进先出)的方式,先执行before,再after,碰到before就执行,没有的话再执行栈中的方法。