Java Static agent JDK Dynamic Agent Cglib dynamic agent

Source: Internet
Author: User

The following is an example of a simple bank account describing a dynamic agent.

Design a bank account, including the user's account balance, to achieve the query and update the balance function

This system has been used for a period of time, the customer asked for account balance to the wrong? Because there is no record of deposit and withdrawal, the last bank is not admit, the customer received the loss. In order to avoid this phenomenon, the bank decided to modify the system, but because the bankaccount is too complicated, I hope to increase the logging function without modifying the bankaccount.

Static proxy

Use static proxies to resolve the above problem.

The bank requires that all modules need to add log function, which is really a heavy workload for the hard-working programmer, how many custom proxy classes do I need to write??? There is no time to play happily!

Dynamic Agent

Java JDK provides a dynamic proxy implementation mechanism, not for each class to manually write a proxy class, it can help you automatically generate proxy classes.

Here are some of the key methods and interfaces used above:

Invocationhandler interface: public interface Invocationhandler {public object Invoke (Object Proxy,method method,object[] args)  Throws Throwable; } parameter Description: Object proxy: An instance of a dynamically generated proxy class

Method: Methods to invoke object[] args: Parameters required for method invocation

The subclass of the Invocationhandler interface can be imagined as a proxy's final operation class, replacing the Proxysubject.

Proxy class: proxy class is specialized to complete the operation of the agent class, you can use this class for one or more interfaces to dynamically generate the implementation class, this class provides the following methods of action: public static Object Newproxyinstance (ClassLoader   Loader, class<?>[] interfaces, Invocationhandler h) throws illegalargumentexception parameter description: ClassLoader Loader: Class loader Class<?>[] Interfaces: Get all interfaces Invocationhandler H: Get a subclass instance of the Invocationhandler interface

Ps: Class loader in the proxy class in the Newproxyinstance () method requires an instance of the ClassLoader class, ClassLoader actually corresponds to the class loader, in Java there are three kinds of loaders;  Booststrap ClassLoader: This loader is written in C + +, in general development is not visible; Extendsion ClassLoader: Used to load extended classes, generally corresponding to the Jre\lib\ext directory of classes; Appclassloader: (default) Loads the class specified by Classpath, which is most commonly used as an loader.

Bind () Creates a proxy class object through the newproxyinstance of the proxy.

By jdkproxyfactory we can see that bind will help you generate a proxy class, regardless of whether bind passes any class. You can quickly meet the requirements of the bank to add logs for each module.

Cglib Dynamic Agent

The dynamic agent mechanism of JDK can only implement the class of the interface, and the class that cannot implement the interface cannot implement the dynamic proxy of the JDK, the cglib is to implement the proxy for the class, his principle is to generate a subclass for the target class, and overwrite the method implementation enhancement, but because inherit is adopted, Therefore, the final decorated class cannot be proxied. Cglib is a third-party implementation.

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.

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.

Static proxy, JDK dynamic agent, Cglib Dynamic agent test classes are as follows:

Source code, documentation, required jar (Public number: Programmer's Road): Http://pan.baidu.com/s/1nuvzfkP

Java Static agent JDK Dynamic Agent 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.