Since JDK 1.3, dynamic proxy has been introduced. JDK dynamic proxy is very simple to use, but it has a limitation that it is an object that uses dynamic proxy.One or more interfaces must be implemented . You can use the CGLIB package for classes that do not implement interfaces on the proxy.
CGLIB is a powerful and high-performance code generation package. It is used by many AOP frameworks (such as Spring AOP) to provide them with interception (blocking) of methods ). Hibernate also uses CGLIB to proxy single-ended (Multiple-to-one and one-to-one) associations. EasyMock uses a moke object to test java code packages. Both use CGLIB to create moke objects for classes without interfaces.
At the underlying layer of the CGLIB package, a small and fast bytecode Processing Framework (ASM) is used to convert bytecode and generate a new class. You are not encouraged to use ASM directly because it requires you to be familiar with the JVM internal structure, including the format and Instruction Set of the class file.
The following is a simulated case where a cglib-nodep-2.1_3.jar package needs to be introduced when CGLIB is used
Define a HelloWorld class. Note that it is a class, not an interface.
Package com. ljq. test;
/**
* Define a HelloWorld class
*
* @ Author jiqinlin
*
*/
Public class HelloWorld {
Public void sayHelloWorld (){
System. out. println ("HelloWorld! ");
}
}
CglibProxy class
Package com. ljq. test;
Import java. lang. reflect. Method;
Import net. sf. cglib. proxy. Enhancer;
Import net. sf. cglib. proxy. MethodInterceptor;
Import net. sf. cglib. proxy. MethodProxy;
/**
* Cglib is used to output two strings to the console before and after method calls.
*
* @ Author jiqinlin
*
*/
Public class CglibProxy implements MethodInterceptor {
// Original object to be proxy
Private Object obj;
Public Object createProxy (Object target ){
This. obj = target;
Enhancer enhancer = new Enhancer ();
Enhancer. setSuperclass (this. obj. getClass (); // sets the proxy target
Enhancer. setCallback (this); // sets the callback.
Enhancer. setClassLoader (target. getClass (). getClassLoader ());
Return enhancer. create ();
}
/**
* Process method calls on the proxy instance and return results
*
* @ Param proxy
* Proxy
* @ Param method
* Proxy method
* @ Param params
* Parameter array of this method
* @ Param methodProxy
*/
Public Object intercept (Object proxy, Method method, Object [] params,
MethodProxy methodProxy) throws Throwable {
Object result = null;
// Before calling
DoBefore ();
// Call the method of the original object
Result = methodProxy. invokeSuper (proxy, params );
// After the call
DoAfter ();
Return result;
}
Private void doBefore (){
System. out. println ("before method invoke ");
}
Private void doAfter (){
System. out. println ("after method invoke ");
}
}
Test class
package com.ljq.test;
public class HelloWorldTest {
public static void main(String[] args) {
HelloWorld helloWorld=new HelloWorld();
CglibProxy cglibProxy=new CglibProxy();
HelloWorld hw=(HelloWorld)cglibProxy.createProxy(helloWorld);
hw.sayHelloWorld();
}
}
The running result is: