[introduction to Java] Day12 Java agent – cglib dynamic agent

Today, let's introduce another more powerful agent, cglib dynamic agent.

What is cglib dynamic proxy?

Let's first review the JDK dynamic proxy in the previous article. JDK dynamic proxy dynamically creates proxy objects of delegate classes at runtime through interfaces. However, like static proxy, it has a disadvantage that it must implement the same interface as the delegate class. When the number of interfaces increases, it needs to increase the number of proxy classes to meet the demand, Moreover, if the delegate class is written by someone else and does not implement any interface, then the JDK dynamic agent is a little weak.

At this time, cglib dynamic proxy stands out. Cglib does not rely on the interface. It can directly generate the proxy object of the delegate class, and can proxy any non final modified public and protected methods of the delegate class. Let's take a look at a chestnut first.

First define a programmer class:

Then define a proxy class:

Then test:

The output is as follows:

The steps of implementing dynamic proxy by cglib are not very troublesome. First, create a class to implement the methodinterceptor interface and rewrite the intercept method. In intercept, you can intercept all public and protected methods of non final modification of the delegate class. In the above example, method invoke(target,params); That is, to call the original method of the original object, the reference of the delegate class object is saved in the proxy class, which is the same as JDK dynamic proxy. The dobefore method is called before calling the original method, and the doafter method is called after calling, so as to realize the proxy function. As for the createProxy method, it is only a fixed step to create Enhance objects first, then entrust some attributes of the delegate class, then call the Create method to dynamically generate proxy objects.

In the test class, in order to more clearly explain the relationship between the proxy class and the delegate class, modify the name field with the proxy class object programmerproxya and the delegate class object programmera respectively, which can produce the same effect.

Let's compare cglib dynamic agent with JDK dynamic agent:

  1. Both are dynamic proxies, which are dynamically generated proxy objects at run time.

  2. JDK dynamic proxy uses the interface information to implement the proxy. The delegate class must implement some or some interfaces, while cglib uses inheritance relationship and ASM to dynamically generate the subclasses of the delegate class at runtime, so as to implement the proxy of the delegate class. Therefore, it does not depend on the interface.

  3. Cglib uses inheritance to implement proxy, so it cannot proxy the final modified classes and methods.

  4. Cglib is generally more efficient than JDK dynamic agent, and the agent that can be implemented is also more powerful.

Of course, according to the specific situation, although cglib is more powerful than JDK dynamic agent, it may not be forcibly used everywhere. Sometimes JDK dynamic agent is relatively simple and rough.

So far, this article is over, and the relevant contents of the agent are explained. Welcome to continue your attention.

Jar package download address: http://download.csdn.net/download/qiuyingjia/10181844

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>