A pre-enhancement, post-enhanced small demo was written in front of it, and the pre-enhancement was enhanced for the method before the method call, and the post-enhancement was enhanced on the method after the method call. Surround enhancement allows the cross-cutting logic to be woven before and after the target class method invocation, which synthesizes the functions of both pre-and post-enhancement.
Also continue to follow the previous code, which describes the implementation classes and test classes that surround the enhancements.
Surround Enhancement 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 target method into parameter object[] args = Invocation.getarguments (); String clientName = (string) args[0];//the target method call before executing System.out.println ("How is you! Mr. "+clientname+". "); /Call the target method through the reflection mechanism object obj = Invocation.proceed ();//System.out.println ("please enjoy yourself!") after the target method call; return obj;}}
There is a difference between the surround-enhanced code and the front-facing enhancement code, and the implementation of the interface-package path has changed. The interface for pre-enhancement and post-enhancement implementations is under the ORG.SPRINGFRAMEWORK.AOP path, while the surround enhancement class needs to implement Org.aopalliance.intercept.MethodInterceptor
interface. This interface is not provided by spring, it is written by the AOP Consortium , and Spring just borrows it. the interface has a unique interface method
Object invoke (Methodinvocation invocation) throws Throwable , methodinvocation It not only encapsulates the target method and its parameter group, but also encapsulates the instance object where the target method resides, and through the Methodinvocation's Getarguments () method, it can get the parameter group of the target method and invoke the corresponding method of the target instance through proceed () reflection. By defining the crosscutting logic in the implementation class, it is easy to implement the front-to-back enhancement 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 Enhanced class methodinterceptor Aroundadvice = new Greetingaroundadvice ();//Spring The agent factory provided is proxyfactory PF = new Proxyfactory ();//Set proxy target pf.settarget (target);//Add Enhanced Pf.addadvice (Aroundadvice) for the proxy target;// Generate proxy instance Waiter proxy = (waiter) pf.getproxy ();p Roxy.greetto ("John");p Roxy.serveto ("Tomcat");}
Output results
Log4j:warn No Appenders could is found for logger (org.springframework.aop.framework.CglibAopProxy). Log4j:warn Initialize the log4j system Properly.log4j:WARN see http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info . How is you! Mr.John.greet to John ... Please enjoy yourself! How is you! Mr.Tomcat.serving Tomcat ... Please enjoy yourself!
Spring AOP Surround Enhancement Small Demo