The JDK implementation dynamic Proxy needs to implement the class through the interface to define the business method, for the class without the interface, how to implement dynamic proxy, this needs cglib. Cglib uses a very low-level bytecode technology, which is based on bytecode technology for a class to create subclasses, and in subclasses using method interception techniques to intercept all the calls of the parent class method, the trend of weaving into the crosscutting logic. Both the JDK dynamic agent and the Cglib Dynamic agent are the basis for implementing Spring AOP.
A simple implementation Example:
This is a class that needs to be proxied, that is, the parent class, through bytecode technology to create subclasses of this class, implementing dynamic proxies.
[Java]View PlainCopy
- Public class SayHello {
- public Void Say () {
- System.out.println ("Hello everyone");
- }
- }
This class implements methods and proxies for creating subclasses. The GetProxy (Superclass.class) method creates a proxy object by extending the class of the parent class by entering the byte code of the parent class. The Intercept () method intercepts the invocation of all target class methods, and obj represents an instance of the target class, which is the reflection object of the target class method, and args is the dynamic entry for the method, and proxy is the agent class instance. Proxy.invokesuper (obj, args) invokes a method in the parent class through the proxy class.
[Java]View PlainCopy
- Public class Cglibproxy implements methodinterceptor{
- Private Enhancer enhancer = new enhancer ();
- Public Object GetProxy (Class clazz) {
- //Set up classes that need to create subclasses
- Enhancer.setsuperclass (Clazz);
- Enhancer.setcallback (this);
- //Dynamic creation of subclass instances by byte-code technology
- return enhancer.create ();
- }
- //Implement Methodinterceptor interface method
- Public Object Intercept (Object obj, Method method, object[] args,
- Methodproxy proxy) throws Throwable {
- System.out.println ("pre-agent");
- //Calling methods in the parent class through the proxy class
- Object result = Proxy.invokesuper (obj, args);
- System.out.println ("post Agent");
- return result;
- }
- }
Specific implementation classes:
[Java]View PlainCopy
- Public class Docglib {
- public static void Main (string[] args) {
- Cglibproxy proxy = new Cglibproxy ();
- //Create a proxy class in a way that generates subclasses
- SayHello proxyimp = (SayHello) proxy.getproxy (SayHello. Class);
- Proxyimp.say ();
- }
- }
Output Result:
[Plain]View PlainCopy
- Front-facing agent
- Hello everyone
- Post-placement Agent
The dynamic proxy object created by Cglib has a much higher performance than the dynamic proxy object created by the JDK, but Cglib spends a lot more time creating proxy objects than the JDK, so for a singleton object, because there is no need to create objects frequently, use cglib appropriately, and vice versa, It is more appropriate to use the JDK approach. At the same time, because Cglib is a method of dynamically creating subclasses, it is not possible to proxy for the final method.
The principle and realization of cglib dynamic agent