Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Example Analysis of dynamic Agent in Spring Framework

Shulou Source: shulou.com Published: 2022-06-01 21:13:24 10月03日 Update

This article mainly shows you the "example analysis of dynamic agents in the Spring framework", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of dynamic agents in the Spring framework".

We have introduced the knowledge related to AOP and learned that spring implements AOP logic through dynamic proxies. In spring, dynamic agents are also divided into two types, one is JDK dynamic agent, the other is CGLib dynamic agent. Let's take a look at the difference between these two dynamic agents. Let's first take a look at the logical processing of adding things when there is no dynamic agent.

Although the above code has implemented our logic, as we introduced in the previous article, the problem with the above code is that repetitive code related to things will be added to the business class, which is not easy to maintain. The solution is to solve the problem through AOP. Below, we use JDK dynamic agent and CGLib dynamic agent technology to solve the above problems.

JDK dynamic agent

The above code uses JDK dynamic proxy technology to achieve AOP function, although it is relatively simple to achieve AOP function through the above code, but one drawback is that JDK proxy can only support interface types, that is, it can only provide dynamic proxy function for interfaces, while CGLib dynamic proxy can directly create proxy classes for classes. Let's implement the above functions through the CGLib dynamic proxy.

CGLib dynamic agent

The difference between JDK dynamic Agent and CGLib dynamic Agent

JDK dynamic proxies can only create proxy classes for interfaces, while CGLib dynamic proxies can create proxy classes directly for classes.

JDK dynamic agents create proxy classes faster than CGLib dynamic agents create proxy classes.

The performance of CGLib dynamic proxy creation proxy class is higher than that of JDK dynamic proxy creation proxy class.

Therefore, when creating a proxy class for a singleton object, we do not need to create a proxy object frequently, so we give priority to using CGLib dynamic proxy to create a proxy class, so that the performance of the proxy class is relatively high, on the contrary, we use JDK dynamic proxy to create a proxy class.

The above is all the content of the article "sample Analysis of dynamic agents in Spring Framework". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Agents dynamics code functions frameworks examples analysis content that is performance interfaces articles logic things objects technology ways knowledge speed problems Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL MariaDB Linux Shulou Technology macOS