Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation principle of SpringAop and what is the agent mode?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the SpringAop implementation principle and agent mode is what the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that after reading this SpringAop implementation principle and agent mode is what the article will have a harvest, let's take a look.

The principle of Spring Aop

Spring's AOP is implemented through dynamic proxies. When aspects are configured for a Bean or some Bean, Spring creates a proxy object for it, and when a method of that object is called, it is actually calling the object method of the generated proxy class. Spring's Aop mainly uses two dynamic agents, namely, JDK's dynamic agent and CGLIB's dynamic agent.

1. JDK dynamic proxy

If the proxy class implements an interface, Spring uses the JDK dynamic proxy by default. The dynamic proxy of JDK is implemented based on reflection. JDK uses reflection to generate a proxy class that implements all the interfaces of the original class and proxies all the methods defined in the interface. When we execute the method of the original class through the proxy object, the bottom layer of the proxy class invokes the invoke method of the InvocationHandler interface that we implement through the reflection mechanism.

/ * * Interface class * / public interface Person {void say ();} * Interface implementation class public class Man implements Person {private String word; public Man (String word) {this.word = word;} public Man () {public void say () {System.out.println ("Man Can Say" + word) Public class ManJDKProxy implements InvocationHandler {/ * required proxy object * / private Object o; public Object bind (Object o) {this.o = o; return Proxy.newProxyInstance (o.getClass (). GetClassLoader (), o.getClass (). GetInterfaces (), this); @ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System.out.println ("JDKProxy Design") Return method.invoke (o, args); / * JDK dynamic agent public class ProxyDesign_2 {public static void main (String [] args) {Man man = new Man ("Hello"); Person p = (Person) new ManJDKProxy () .bind (man); p.say ()

* advantages and disadvantages of JDK dynamic agent

Advantages:

1. JDK dynamic proxy is native to JDK and can be used without any dependency.

two。 The speed of generating proxy class through reflection mechanism is faster than that of CGLib operation bytecode.

Disadvantages:

1. If you want to use a JDK dynamic proxy, the class being proxied must implement the interface, otherwise it cannot be InvocationHandler

2. JDK dynamic proxy cannot implement a proxy for methods that are not defined in the interface

3. When the JDK dynamic agent executes the proxy method, it needs to be called back through the reflection mechanism, so the efficiency of the method execution is relatively low.

2. CGLIB dynamic proxy

If the class that needs the proxy does not implement the interface, JDK's dynamic proxy cannot be used, and Spring uses the CGLiB dynamic proxy to generate the proxy object. CGLiB directly manipulates the bytecode, generates subclasses of the class, and overrides the methods of the class to complete the proxy.

/ * * Interface class * / public interface Person {void say ();} * API implementation class public class Man implements Person {private String word; public Man (String word) {this.word = word;} public Man () {public void say () {System.out.println ("Man Can Say" + word); public class ManCGLIBProxy {public Object bind (Object target) {Enhancer enhancer = new Enhancer () Enhancer.setSuperclass (target.getClass ()); enhancer.setCallback (new MethodInterceptor () {@ Override public Object intercept (Object o, Method method, Object [] objects, MethodProxy methodProxy) throws Throwable {System.out.println ("CGLIB Proxy Design"); return method.invoke (target, objects);}}); return enhancer.create () / * * CGLIB dynamic agent public class ProxyDesign_3 {public static void main (String [] args) {Man man = new Man ("Hello"); Person p = (Person) new ManCGLIBProxy () .bind (man); p.say ()

* advantages and disadvantages of CGLiB dynamic agent

Advantages:

1. Classes that use CGLiB proxies do not need to implement the interface, because the proxy classes generated by CGLib inherit directly from the classes that need to be proxied

two。 Because the CGLiB implementation overrides the methods of the parent class, there is no way to proxy final methods or private methods

3. CGLiB is a proxy class generated by modifying bytecode, so the efficiency of CGLib is higher than that of JDK dynamic proxy.

Disadvantages:

1. Because the CGLiB implementation overrides the methods of the parent class, there is no way to proxy final methods or private methods

two。 Because CGLiB generates proxy classes by manipulating bytecode (asm toolkit), this way of generating proxy classes is less efficient than that of JDK generating proxy classes through reflection

3. How to enforce the use of CGLIB proxy in Spring projects

* xml mode

* @ Aspect annotation method

* configure the annotation method of class

Add @ EnableAspectJAutoProxy (proxyTargetClass = true) to introduce the article on "what is the principle of SpringAop implementation and proxy pattern". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the principle of SpringAop implementation and agent mode". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report