When Net.sf.cglib.proxy.MethodInterceptor is a callback (callback) for all proxy methods, this method is called before the method of the original object is called when it is called on a proxy-based method, as shown in 3. The first parameter is the proxy pair image, the second and third parameters are the parameters of the intercepted method and method, respectively. The original method may be called by using the general reflection of the Java.lang.reflect.Method object, or by using the Net.sf.cglib.proxy.MethodProxy object. Net.sf.cglib.proxy.MethodProxy is usually preferred because it is faster. In this method, we can inject our own code before or after invoking the original method.
figure 3:cglib methodinterceptor
net.sf.cglib.proxy.methodinterceptor is able to meet any interception (interception) need, while for some cases may be excessive. To simplify and improve performance, the Cglib package provides a number of specialized callback (callback) types. For example:
net.sf.cglib.proxy.FixedValue
To improve performance, the Fixedvalue callback is useful for forcing a particular method to return a fixed value.
net.sf.cglib.proxy.NoOp
The NOOP callback directly delegates the method invocation to the implementation of this method in the parent class.
net.sf.cglib.proxy.LazyLoader
You can use the Lazyloader callback when the actual object requires a lazy mount. Once the actual object is loaded, it will be used by each method that invokes the proxy object.
net.sf.cglib.proxy.Dispatcher
The Dispathcer callback and the Lazyloader callback have the same characteristics, but the method of loading the object is always called when the proxy method is called.
net.sf.cglib.proxy.ProxyRefDispatcher
The Proxyrefdispatcher callback, like the dispatcher, differs in that it can pass the proxy object as a parameter to the Load object method.
3, the method of proxy class often uses callbacks (callback), and when you can also use Net.sf.cglib.proxy.CallbackFilter to select some methods using Callbacks (callback), This thoughtful control feature is not in the dynamic agent of the JDK. In the JDK proxy, the invocation of the Java.lang.reflect.InvocationHandler method is valid for the method of the proxy class.
In addition to the proxy class, Cglib implements a proxy to the interface by providing a drop-in substitution for the java.lang.reflect.Proxy. Since the substitution of this proxy capability is seldom used, the corresponding APIs are seldom mentioned.
Cglib's agent package also provides support for Net.sf.cglib.proxy.Mixin. Basically, it allows multiple objects to be bound to a single large object. The invocation of the method in the proxy is delegated to the appropriate object below.
Next we look at how to create a proxy using Cglib proxy APIs.
Create a simple proxy cglib the core of the agent is the Net.sf.cglib.proxy.Enhancer class, in order to create a proxy, at least you need to use this class. First, let's create a proxy using the NoOp callback:
/**
* Create a proxy using NoOpCallback. The target class
* Must has a default zero-argument constructor.
*
* @param targetclass the Super class of the proxy
* @return A new proxy for a target class instance
*/
Public Object Createproxy (Class targetclass) {
Enhancer enhancer = new enhancer ();
Enhancer.setsuperclass (Targetclass);
Enhancer.setcallback (noop.instance);
return Enhancer.create ();
}
The return value is the proxy for one instance of the target class. In this example, we have configured a single callback (callback) for Net.sf.cglib.proxy.Enhancer. We can see that it is very rare to create a simple proxy directly, but rather to create an instance of Net.sf.cglib.proxy.Enhancer, in which you can create a simple proxy using the static helper method in the Net.sf.cglib.proxy.Enhancer class. It is generally recommended to create a proxy using the above example, as it allows you to control the creation of agents by configuring Net.sf.cglib.proxy.Enhancer instances.
It is important to note that the target class is passed in as the parent class of the resulting proxy. Unlike the JDK dynamic agent, which cannot create a proxy simultaneous target object, the target object must be created by the Cglib package. In this example, the default parameterless constructor is used to create the target instance. If you want to use Cglib to create an instance with parameters, use Net.sf.cglib.proxy.Enhancer.create (class[], object[]) Method instead of Net.sf.cglib.proxy.Enhancer.create () is available. The first parameter in the method defines the type of the parameter, and the second is the value of the parameter. In parameters, the base type should be converted to the type of the class.
------------
If you want to load, please specify the source
[@more @]
Create a dynamic proxy using the Cglib package (2) (GO)