This example is a continuation of the previous IOC example.
Add Class 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");
}
}
Add Class 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");
}
}
Add Class 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;
}
}
Modify the Mybeans.xml file as follows
<?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>
Test class 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");//The test is identical to the ID of the bean of the proxy class in the XML configuration file
TService tservice = new TService ();
Tservice.printname (Tinterface);
System.out.println (Tinterface.setname ("BBB"));
}
}
The results of the run normally show enhanced information. The enhanced granularity is class, not to the method.
In addition, the red portion of the <list> label in the XML configuration file shows the order of the enhanced code that displays the output differently, depending on the order. Details are as follows:
First type:
<value>MyBeforeAdvice</value>
<value>MyAroundAdvice</value>
<value>MyAfterAdvice</value>
The output is:
Mybeforeadvice:before
Myaroundadvice:before
=======getname===========
Myafteradvice:afterreturning
Myaroundadvice:after
The second type:
<value>MyAroundAdvice</value>
<value>MyBeforeAdvice</value>
<value>MyAfterAdvice</value>
The output is:
Myaroundadvice:before
Mybeforeadvice:before
=======getname===========
Myafteradvice:afterreturning
Myaroundadvice:after
The third type:
<value>MyBeforeAdvice</value>
<value>MyAroundAdvice</value>
<value>MyAfterAdvice</value>
The output is:
Mybeforeadvice:before
Myaroundadvice:before
=======getname===========
Myafteradvice:afterreturning
Myaroundadvice:after
The fourth kind:
<value>MyAroundAdvice</value>
<value>MyBeforeAdvice</value>
<value>MyAfterAdvice</value>
The output is:
Myaroundadvice:before
Mybeforeadvice:before
=======getname===========
Myafteradvice:afterreturning
Myaroundadvice:after
The fifth kind:
<value>MyAroundAdvice</value>
<value>MyAfterAdvice</value>
<value>MyBeforeAdvice</value>
The output is:
Myaroundadvice:before
Mybeforeadvice:before
=======getname===========
Myafteradvice:afterreturning
Myaroundadvice:after
The sixth kind:
<value>MyAfterAdvice</value>
<value>MyAroundAdvice</value>
<value>MyBeforeAdvice</value>
The output is:
Myaroundadvice:before
Mybeforeadvice:before
=======getname===========
Myaroundadvice:after
Myafteradvice:afterreturning
The implementation of the order of my opinion: the use of the stack (LIFO), the first implementation of before, and then after, encounter before on the implementation, there is no further implementation of the Stack method.