Java Dynamic proxy 2-CGLIB dynamic proxy application

Source: Internet
Author: User

Since JDK 1.3, dynamic proxy has been introduced. JDK dynamic proxy is very simple to use, but it has a limitation that it is an object that uses dynamic proxy.One or more interfaces must be implemented . You can use the CGLIB package for classes that do not implement interfaces on the proxy.
CGLIB is a powerful and high-performance code generation package. It is used by many AOP frameworks (such as Spring AOP) to provide them with interception (blocking) of methods ). Hibernate also uses CGLIB to proxy single-ended (Multiple-to-one and one-to-one) associations. EasyMock uses a moke object to test java code packages. Both use CGLIB to create moke objects for classes without interfaces.
At the underlying layer of the CGLIB package, a small and fast bytecode Processing Framework (ASM) is used to convert bytecode and generate a new class. You are not encouraged to use ASM directly because it requires you to be familiar with the JVM internal structure, including the format and Instruction Set of the class file.

The following is a simulated case where a cglib-nodep-2.1_3.jar package needs to be introduced when CGLIB is used

Define a HelloWorld class. Note that it is a class, not an interface.

Package com. ljq. test;

/**
* Define a HelloWorld class
*
* @ Author jiqinlin
*
*/
Public class HelloWorld {
Public void sayHelloWorld (){
System. out. println ("HelloWorld! ");
}
}

CglibProxy class

Package com. ljq. test;

Import java. lang. reflect. Method;

Import net. sf. cglib. proxy. Enhancer;
Import net. sf. cglib. proxy. MethodInterceptor;
Import net. sf. cglib. proxy. MethodProxy;

/**
* Cglib is used to output two strings to the console before and after method calls.
*
* @ Author jiqinlin
*
*/
Public class CglibProxy implements MethodInterceptor {
// Original object to be proxy
Private Object obj;

Public Object createProxy (Object target ){
This. obj = target;
Enhancer enhancer = new Enhancer ();
Enhancer. setSuperclass (this. obj. getClass (); // sets the proxy target
Enhancer. setCallback (this); // sets the callback.
Enhancer. setClassLoader (target. getClass (). getClassLoader ());
Return enhancer. create ();
}


/**
* Process method calls on the proxy instance and return results
*
* @ Param proxy
* Proxy
* @ Param method
* Proxy method
* @ Param params
* Parameter array of this method
* @ Param methodProxy
*/
Public Object intercept (Object proxy, Method method, Object [] params,
MethodProxy methodProxy) throws Throwable {
Object result = null;
// Before calling
DoBefore ();
// Call the method of the original object
Result = methodProxy. invokeSuper (proxy, params );
// After the call
DoAfter ();
Return result;
}

Private void doBefore (){
System. out. println ("before method invoke ");
}

Private void doAfter (){
System. out. println ("after method invoke ");
}

}

Test class

package com.ljq.test;

public class HelloWorldTest {

public static void main(String[] args) {
HelloWorld helloWorld=new HelloWorld();
CglibProxy cglibProxy=new CglibProxy();
HelloWorld hw=(HelloWorld)cglibProxy.createProxy(helloWorld);
hw.sayHelloWorld();
}
}

The running result is:

 

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.