Spring AOP surround enhancement Demo
I wrote a demo of pre-enhancement and post-enhancement. The pre-enhancement is the method enhancement before the method call, and the post-enhancement is the method enhancement after the method call. Surround enhancement allows the cross-cutting logic to be woven before and after the call of the target class method. It combines the functions of the front and back sides to enhance the functions of both.
We will continue to use the previous Code. Here we will introduce the enhanced implementation class and test class.
GreetingAroundAdvice. java
Package com. paic. zhangqi. spring. aop; import org. aopalliance. intercept. methodInterceptor; import org. aopalliance. intercept. methodInvocation; public class GreetingAroundAdvice implements MethodInterceptor {@ Overridepublic Object invoke (MethodInvocation invocation) throws Throwable {// get the Object parameter [] args = invocation. getArguments (); String clientName = (String) args [0]; // run System before calling the target method. out. println ("Ho W are you! Mr. "+ clientName + ". "); // call the target method Object obj = invocation through the reflection mechanism. proceed (); // after the target method is called, run System. out. println ("Please enjoy yourself! "); Return obj ;}}
The code for surround enhancement is different from the code for the front enhancement, and the path of the implemented interface package is changed. The interfaces for pre-enhancement and post-enhancement areOrg. springframework. aopPath, while the surround enhancement class needs to be implementedOrg. aopalliance. intercept. MethodInterceptor
Interface. This interface is not provided by Spring. It is written by the AOP alliance, and Spring only borrows it. This interface has a unique interface method.
Object invoke (MethodInvocation invocation) throws ThrowableMethodInvocation not only encapsulates the target method and its input parameter group, but also encapsulates the Instance Object of the target method. You can obtain the input parameter group of the target method through the getArguments () method of MethodInvocation, call the corresponding method of the target instance through proceed () reflection. By defining the cross-cutting logic in the implementation class, it is easy to enhance the front and back of the method.
Test class TestAdvice. java
Package com. paic. zhangqi. spring. aop; import org. aopalliance. intercept. methodInterceptor; import org. springframework. aop. framework. proxyFactory; public class TestAdvice {public static void main (String [] args) {Waiter target = new NaiveWaiter (); // surround enhancement class MethodInterceptor aroundAdvice = new GreetingAroundAdvice (); // spring provides the proxy factory ProxyFactory pf = new ProxyFactory (); // sets the proxy target pf. setTarget (target); // Add an enhanced pf for the proxy target. addAdvice (aroundAdvice); // generate the proxy instance Waiter proxy = (Waiter) pf. getProxy (); proxy. greetTo ("John"); proxy. serveTo ("Tomcat ");}}
Output result
log4j:WARN No appenders could be found for logger (org.springframework.aop.framework.CglibAopProxy).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.How are you!Mr.John.greet to John...Please enjoy yourself!How are you!Mr.Tomcat.serving Tomcat...Please enjoy yourself!