Simple demo of JDK dynamic proxy and CGLib dynamic proxy

Source: Internet
Author: User

After JDK1.3, Java provides dynamic proxy technology that allows developers to create proxy instances for interfaces during runtime.

I. First, we will demonstrate JDK dynamic proxy.

Now we have a simple business interface Saying, as shown below:

 
 
 
 
 

A simple implementation class SayingImpl is as follows:

SayingImpl System. out. println (name + ": Hello! "System. out. println (name +": I mean, we have to work hard to build a harmonious society! "

What we need to achieve is to dynamically implant data before and after sayHello and talking.

JDK dynamic Proxy mainly uses two classes in the java. lang. reflect package: Proxy and InvocationHandler.

InvocationHandler is an interface that defines the cross-cutting logic by implementing this interface, and calls the code of the target class through the reflection mechanism, dynamically weaving the cross-cutting logic and the business logic together.

The Proxy uses InvocationHandler to dynamically create an instance that complies with a certain interface and generate the Proxy object of the target class.

Create an InvocationHandler instance as follows:

MyInvocationHandler. target = System. out. println ("--------------------------" "next digit, please speak on stage! "Object obj = System. out. println (" Welcome! "

The following is a test:

Saying target = MyInvocationHandler handler = Saying proxy = target. getClass (). getInterfaces (), handler); proxy. sayHello ("James" "Lili"

The running status is as follows:

 

JDK dynamic proxy has a large limitation, that is, it requires that the target class must implement the interface of the corresponding method, and it can only create a proxy instance for the interface. We can see in the newProxyInstance method of Proxy in the test class above that the second parameter of this method is the interface of the target class. If this class does not implement the interface, cglib dynamic proxy is required.

CGLib uses a very low-level bytecode technology. It can create a subclass for a class and use the method Blocking Technology in the subclass to intercept calls of all parent class methods and implant the cross-cutting logic.

 

Ii. Next we will demonstrate cglib dynamic proxy.

First we need to export the package. The package I use is a cglib-nodep-2.1_3.jar.

First, create a proxy maker CglibProxy:

CglibProxy = System. out. println ("--------------------------" "Next one, please speak on stage! "Object result = System. out. println (" Applause! "

Then perform the test:

= Saying target = (Saying) proxy. getProxy (SayingImpl. "James" "Lili"

The result is no different from that of JDK dynamic proxy.

Both JDK dynamic proxy and CGLib dynamic proxy are enhanced during runtime. They are enhanced by embedding the cross-section code into the proxy class. In contrast to AspectJ, it can implant and enhance the cross-cutting code during compilation through a special compiler, which is more advantageous at runtime, because JDK dynamic proxy and CGLib dynamic proxy need to be enhanced every time they run.

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.