The principle and realization of cglib dynamic agent

Source: Internet
Author: User

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
    1. Public class SayHello {
    2. public Void Say () {
    3. System.out.println ("Hello everyone");
    4. }
    5. }

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
  1. Public class Cglibproxy implements methodinterceptor{
  2. Private Enhancer enhancer = new enhancer ();
  3. Public Object GetProxy (Class clazz) {
  4. //Set up classes that need to create subclasses
  5. Enhancer.setsuperclass (Clazz);
  6. Enhancer.setcallback (this);
  7. //Dynamic creation of subclass instances by byte-code technology
  8. return enhancer.create ();
  9. }
  10. //Implement Methodinterceptor interface method
  11. Public Object Intercept (Object obj, Method method, object[] args,
  12. Methodproxy proxy) throws Throwable {
  13. System.out.println ("pre-agent");
  14. //Calling methods in the parent class through the proxy class
  15. Object result = Proxy.invokesuper (obj, args);
  16. System.out.println ("post Agent");
  17. return result;
  18. }
  19. }

Specific implementation classes:

[Java]View PlainCopy
    1. Public class Docglib {
    2. public static void Main (string[] args) {
    3. Cglibproxy proxy = new Cglibproxy ();
    4. //Create a proxy class in a way that generates subclasses
    5. SayHello proxyimp = (SayHello) proxy.getproxy (SayHello.   Class);
    6. Proxyimp.say ();
    7. }
    8. }

Output Result:

[Plain]View PlainCopy
    1. Front-facing agent
    2. Hello everyone
    3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.