Java Dynamic Agent (ii) CGLIB Dynamic agent Application

Source: Internet
Author: User

JDK since the 1.3 release, the introduction of dynamic agents, the JDK dynamic proxy is very simple to use, but it has a limitation that the use of dynamic proxy objects must implement one or more interfaces . If you want to delegate a class that does not implement an interface, you can use the Cglib package.
Cglib is a powerful, high-performance code-generation package. It is used by many AOP frameworks (such as spring AOP) to provide them with a method of interception (interception). Hibernate also uses Cglib to proxy single-ended single-ended (many-to-one and a-to-a) association. Easymock a package that tests Java code by using a mock (Moke) object. They all create mock-up (Moke) objects for classes that do not have interfaces by using Cglib.
The bottom of the Cglib package is to convert bytecode and generate new classes by using a small, fast bytecode processing framework asm. Direct use of ASM is discouraged because it requires you to be familiar with the format and instruction set of the JVM internal structure including the class file.

The following is a simulation case where the Cglib-nodep-2.1_3.jar package needs to be introduced when using Cglib

Define a HelloWorld class, note that this 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;

/**
* Output two lines of string to the console before and after a method call through the Cglib implementation
*
* @author Jiqinlin
*
*/
public class Cglibproxy implements Methodinterceptor {
The original object to be proxied
Private Object obj;

public object Createproxy (object target) {
This.obj = target;
Enhancer enhancer = new enhancer ();
Enhancer.setsuperclass (This.obj.getClass ());//Set proxy target
Enhancer.setcallback (this);//Set callback
Enhancer.setclassloader (Target.getclass (). getClassLoader ());
return Enhancer.create ();
}


/**
* Process the method call on the proxy instance and return the result
*
* @param proxy
* Proxy class
* @param method
* Method of being represented
* @param params
* parameter array for this method
* @param methodproxy
*/
public object Intercept (object proxy, Method method, object[] params,
Methodproxy methodproxy) throws Throwable {
Object result = null;
Before calling
Dobefore ();
Methods to invoke 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 result of the operation is:

Java Dynamic Agent (ii) CGLIB Dynamic agent Application

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.