Actual Combat Cglib Series articles
This article introduces the implementation of a dynamic proxy through methodinterceptor and enhancer.
First, talk about the dynamic agents in the JDK:
The dynamic proxy in JDK is realized by reflection class proxy and Invocationhandler callback interface, please refer to my article Blog:java Dynamic agent for understanding http://shensy.iteye.com/blog/1698197
However, the class of the dynamic proxy to be implemented in the JDK must implement an interface, that is, to proxy only the methods defined in the interface implemented by the class, which has some limitations in practical programming and is not very efficient in using reflection.
Two, use Cglib to realize:
Using Cglib to implement dynamic proxies is completely unaffected by the need for proxy classes to implement interfaces, and the Cglib base uses the ASM bytecode generation framework to generate proxy classes using bytecode technology, which is much more efficient than using Java reflection. The only thing to note is that Cglib cannot broker a method that is declared final because the Cglib principle is to dynamically generate subclasses of the proxy class.
Below, an example is introduced to implement dynamic proxies using Cglib.
1, by the proxy class:
First, define a class that does not implement any interfaces and contains two methods.
Java code
public class Concreteclassnointerface {
public string Getconcretemethoda (String str) {
System.out.println ("Concretemethod A ...") "+str);
return str;
}
public int Getconcretemethodb (int n) {
System.out.println ("Concretemethod B ...") "+n);
return n+10;
}
}
2, Interceptor:
Define an interceptor. When the target method is invoked, Cglib invokes the Methodinterceptor interface method intercept to implement your own proxy logic, similar to the Invocationhandler interface in the JDK.
Java code
public class Concreteclassinterceptor implements methodinterceptor{
Public Object intercept (object obj, Method method, object[] arg, Methodproxy proxy) throws Throwable {
System.out.println ("Before:" +method);
Object object=proxy.invokesuper (obj, arg);
System.out.println ("After:" +method);
return object;
}
}
Parameter: Object is a dynamically generated proxy class instance of Cglib, which is referenced by the Proxy method invoked by the entity class above, object[] as a list of parameter values, methodproxy a proxy reference to the method for the generated proxy class.
Return: The value returned from the method call of the proxy instance.
Among them, Proxy.invokesuper (OBJ,ARG):
Invokes the parent-class method of the proxy method on an instance of the surrogate class (i.e., the corresponding method in the entity class Concreteclassnointerface)
In this example, only one word is printed before and after invoking the proxy class method, and of course the actual programming can be other complex logic.
3, generate dynamic proxy class:
Java code
Enhancer enhancer=new Enhancer ();
Enhancer.setsuperclass (Concreteclassnointerface.class);
Enhancer.setcallback (New Concreteclassinterceptor ());
Concreteclassnointerface ccni= (Concreteclassnointerface) enhancer.create ();
Here the enhancer class is a byte-code enhancer in Cglib that can easily extend to the classes you want to work with, and you'll see it often later.
First, the proxy class Concreteclassnointerface is set to the parent class, then the Interceptor Concreteclassinterceptor is set up, and the last Enhancer.create () dynamically generates a proxy class. and is coerced from object to the parent type Concreteclassnointerface.
Finally, the method is invoked on the proxy class:
Java code
Ccni.getconcretemethoda ("Shensy");
Ccni.getconcretemethodb (0);
To view the console output:
Console code
Before:p ublic java.lang.String Generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodA (java.lang.String)
Concretemethod A ... shensy
After:p ublic java.lang.String Generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodA (java.lang.String)
Before:p ublic int Generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodB (int)
Concretemethod B ... 0
After:p ublic int Generic.cglib.proxy.ConcreteClassNoInterface.getConcreteMethodB (int)
As you can see, the interceptor performs a print operation before and after invoking the proxy class method.