[In-depth research on Java] 6. Detailed explanation of CGLib dynamic proxy mechanism, in-depth research on cglib

Source: Internet
Author: User

[In-depth research on Java] 6. Detailed explanation of CGLib dynamic proxy mechanism, in-depth research on cglib

I. First, let's talk aboutDynamic proxy in JDK:

The dynamic Proxy in JDK is implemented through the reflection Proxy and the InvocationHandler callback interface.

However, the class for dynamic proxy in JDK must implement an interface, that is, only the methods defined in the interfaces implemented by this class can be proxy, which has certain limitations in actual programming, in addition, reflection is not very efficient.

Ii. UseCGLib implementation:

Using CGLib to implement dynamic proxy is not limited by the interface that the proxy class must implement. Besides, the bottom layer of CGLib uses the ASM bytecode generation framework and uses bytecode technology to generate proxy classes, which is more efficient than using Java reflection. The only thing to note is that CGLib cannot proxy the methods declared as final, because the principle of CGLib is to dynamically generate subclass of the proxy class.

The following describes how to use CGLib to implement dynamic proxy through an instance.

1. Proxy:

First, define a class that does not implement any interfaces and contains two methods.

Java code
  1. Public class ConcreteClassNoInterface {
  2. Public String getConcreteMethodA (String str ){
  3. System. out. println ("ConcreteMethod A..." + str );
  4. Return str;
  5. }
  6. Public int getConcreteMethodB (int n ){
  7. System. out. println ("ConcreteMethod B..." + n );
  8. Return n + 10;
  9. }
  10. }

2. interceptor:

Define an interceptor. When calling the target method, CGLib calls back the MethodInterceptor interface method interception to implement your own proxy logic, similar to the InvocationHandler interface in JDK.

Java code
  1. Public class ConcreteClassInterceptor implements MethodInterceptor {
  2. Public Object intercept (Object obj, Method method, Object [] arg, MethodProxy proxy) throws Throwable {
  3. System. out. println ("Before:" + method );
  4. Object object = proxy. invokeSuper (obj, arg );
  5. System. out. println ("After:" + method );
  6. Return object;
  7. }
  8. }

Parameter: the Object is a proxy instance dynamically generated by CGLib. The Method is referenced by the proxy Method called by the entity class above, and the Object [] is the parameter value list, methodProxy is the proxy reference of the generated proxy class to the method.

Returned value: the value returned from the method call of the proxy instance.

Here, proxy. invokeSuper (obj, arg ):

Call the parent class method of the proxy method on the proxy instance (that is, the corresponding method in the ConcreteClassNoInterface of the object class)

In this example, only one sentence is printed before and after the method called by the proxy class. Of course, other complex logic can be used in actual programming.

3. Generate a dynamic proxy class:

Java code
  1. Enhancer enhancer = new Enhancer ();
  2. Enhancer. setSuperclass (ConcreteClassNoInterface. class );
  3. Enhancer. setCallback (new ConcreteClassInterceptor ());
  4. ConcreteClassNoInterface ccni = (ConcreteClassNoInterface) enhancer. create ();

Here, the Enhancer class is a bytecode booster in CGLib, which can easily expand the class you want to process and will often see it later.

First, the proxy class ConcreteClassNoInterface is set as the parent class, then the interceptor ConcreteClassInterceptor is set, and then enhancer. create () is executed to dynamically generate a proxy class, and the Object is forced to convert to the parent class ConcreteClassNoInterface.

Finally, call the method on the proxy class:

Java code
  1. Ccni. getConcreteMethodA ("shensy ");
  2. Ccni. getConcreteMethodB (0 );

View console output:

Console code
  1. Before: public java. lang. String generic. cglib. proxy. ConcreteClassNoInterface. getConcreteMethodA (java. lang. String)
  2. ConcreteMethod A... shensy
  3. After: public java. lang. String generic. cglib. proxy. ConcreteClassNoInterface. getConcreteMethodA (java. lang. String)
  4. Before: public int generic. cglib. proxy. ConcreteClassNoInterface. getConcreteMethodB (int)
  5. ConcreteMethod B... 0
  6. After: public int generic. cglib. proxy. ConcreteClassNoInterface. getConcreteMethodB (int)

As you can see, the interceptor executes the print operation before and after calling the proxy method.

Related Article

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.