|
Benpian record both of Spring AOP proxy, do the next foreshadowing the next AOP implementation.
1.JDK dynamic proxy agent 2.cglib
1, if the target object implements the interface, by default, will use the JDK dynamic proxy AOP realization
2, if the target object implements the interface, you can force the use of CGLIB achieve AOP
3, if the target object does not implement an interface, you must use CGLIB library, spring automatically between JDK dynamic proxies and CGLIB conversion
Note: JDK dynamic proxy than cglib agent execution speed, but the performance is not as good cglib. Therefore, the choice of which agent to use will depend on the specific circumstances of a single case of the general model with cglib better.
one. JDK dynamic proxy implementation (works by using reflection)
First define the interface and achieve
public interface TestService {
public int add ();
}
public class TestServiceImpl implements TestService {
@Override
public int add () {
System.out.println ( "begin add ...");
return 0;
}
}
Define the proxy class, to note here is to import packages import java.lang.reflect. *
public class JDKDynamicProxy implements InvocationHandler {
// The target object to be proxied
private Object proxyObj;
/ **
* Object newProxyInstance (ClassLoader loader, Class < ?> [] Interfaces, InvocationHandler h)
* Loader: a class loader ClassLoader objects, which are defined by the ClassLoader object is to load the resulting proxy object
* Interfaces: an array Interface object represents is that I will give an object I need the agent to provide a set of what the interface, if I provide a set of interfaces to it, then the proxy has stated that implements this interface (polymorphism) so I can call this method a set of interfaces
* H: a InvocationHandler object representing this is when my dynamic proxy object when calling the method, the object will be associated where a InvocationHandler
* /
public Object newProxy (Object proxyObj) {
this.proxyObj = proxyObj;
// Returns a proxy object
return Proxy.newProxyInstance (proxyObj.getClass (). getClassLoader (),
proxyObj.getClass (). getInterfaces (),
this);
}
/ **
* Implementation Audience
* Object proxy: proxied objects
* Method method: the method to call
* Object args []: method calls require parameters
* /
@Override
public Object invoke (Object proxy, Method method, Object [] args)
throws Throwable {
before ();
// Method call the target object through the reflection mechanism; Object object = method.invoke (this.proxyObj, args)
the After ();
return object;
}
public void before () {
System.out.println ( "Before beginning the audience ...");
}
public void after () {
System.out.println ( "audience started after ...");
}
}
Test categories:
public static void main (String [] args) {
// We want real object proxy
TestService testService = new TestServiceImpl ();
//testJDKProxyService.add();// not by proxy
JDKDynamicProxy JDKDynamicProxyTarget = new JDKDynamicProxy ();
TestService testServiceProxy = (TestService) JDKDynamicProxyTarget.newProxy (testService);
// Execute the proxy class method
testServiceProxy.add ();
}
two. CGLIB agent, you need to import cglib-nodep-2.1_3.jar
Under the first cglib, CGlib is a powerful, high-performance, high-quality Code generation libraries. It can be extended at runtime Java classes and implement Java interfaces.
Define an implementation class (note that does not implement the interface)
public class TestCGLIBServiceImpl {
public int add () {
System.out.println ( "begin add ...");
return 0;
}
}
Cglib proxy class is defined, then the package should be introduced import net.sf.cglib.proxy. *
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class CGLIBProxy implements MethodInterceptor {
private Object targetObject; // audience proxied
public Object createProxyInstance (Object targetObject) {
. This targetObject = targetObject;
Enhancer enhancer = new Enhancer ();
enhancer.setSuperclass (targetObject.getClass ()); // set the proxy target
enhancer.setCallback (this); // set callback
return enhancer.create ();
}
/ **
* Process method call and returns the result on a proxy instance
* @param Object: proxy class
* @param Method: proxied method
* @param Args: an array of arguments of the method
* @param MethodProxy
* /
@Override
public Object intercept (Object object, Method method, Object [] args,
MethodProxy methodproxy) throws Throwable {
Object result = null;
try {
. System out .println ( "pre-processing start ...");
// Method to perform target object; result = methodproxy.invoke (targetObject, args)
. System out .println ( "post-process started ...");
} Catch (Exception e) {
. System out .println ( "Exception Handling ...");
} Finally {
. System out .println ( "call to end ...");
}
return result;
}
}
Test categories:
public class TestCGLIBProxy {
public static void main (String [] args) {
// We want real object proxy
TestCGLIBServiceImpl testCGLIB = new TestCGLIBServiceImpl ();
CGLIBProxy CGLIBproxy = new CGLIBProxy ();
TestCGLIBServiceImpl testCGLIBProxy = (TestCGLIBServiceImpl) CGLIBproxy.createProxyInstance (testCGLIB);
testCGLIBProxy.add ();
}
}
Written on the back: two kinds of agents spring AOP implementation code to write this here just realized, if you want to really understand, which have to be familiar with the principles of mechanisms, such as reflection, newProxyInstance (...), Enhancer () principle, invoke () and so on principle. |
|
|
|