In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what is the concept of AOP and the principle of the two dynamic agent modes in Spring". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Brief introduction of Conceptual 1.AOP Technology
AOP is the abbreviation of Aspect Oriented Programming, which means aspect-oriented programming. It is a technology to realize the unified maintenance of program functions through precompilation and run-time dynamic agents.
AOP is the continuation of OOP, is a hot spot in software development, is also an important content of Spring framework, and is a derivative paradigm of functional programming. Each part of the business logic can be isolated by using AOP, which reduces the coupling between the various parts of the business logic, improves the reusability of the program, and improves the efficiency of development at the same time.
Advantages of 2.AOP
Function: to enhance the function of the method without modifying the source code while the program is running: reduce repetitive code, improve development efficiency, and facilitate maintenance
3.Spring AOP terminology
The bottom layer of the AOP implementation of Spring is to encapsulate the above dynamic agent code. after the encapsulation, we only need to code the parts that need to be concerned, and enhance the method to complete the specified goal by way of configuration. Before formally explaining the operation of AOP, we must understand the relevant terms of AOP, which are commonly used as follows:
Target (target object): agent's target object Proxy (proxy): when a class is enhanced by AOP weaving, it produces a result proxy class Joinpoint (join point): the so-called join point refers to those points that are intercepted. In spring, these points refer to methods, because spring only supports join points of method types. (method that may be enhanced) Pointcut (pointcut): the so-called pointcut refers to the definition of which Joinpoint we want to intercept (enhanced method) Advice (notification / enhancement): the so-called notification means that what we have to do after intercepting the Joinpoint is to notify (method enhancement for the target object) Aspect (aspect): is the combination of pointcut and notification (introduction) (target method + enhancement = cut) Weaving: the process of applying enhancements to the target object to create a new proxy object. Spring uses dynamic proxy weaving, while AspectJ uses compile-time weaving and class-loading weaving. (the process of combining an action, pointcut and notification = weaving)
Clear matters for 4.AOP development
The content that needs to be written is to write the core business code (the target method of the target class) to write the aspect class, and there are notifications (enhanced function methods) in the aspect class. In the configuration file, configure the weaving relationship, that is, which notifications and which connection points to implement the content Spring framework combined with AOP technology to monitor the implementation of the pointcut method. Once the monitoring pointcut method is run, the agent mechanism is used to dynamically create the proxy object of the target object. according to the notification category, the corresponding function of the notification is woven into the corresponding position of the proxy object to complete the complete code logic operation. Which proxy method is used by the underlying AOP in spring, the framework will decide which dynamic proxy method to use based on whether the target class implements the interface or not.
2.AOP underlying implementation
In fact, the underlying layer of AOP is implemented through the dynamic proxy technology provided by Spring. During the operation, Spring dynamically generates the proxy object through the dynamic proxy technology, intervenes the enhanced function when the method of the proxy object is executed, and invokes the method of the target object, so as to enhance the function.
Dynamic proxy technology of 1.AOP:
Commonly used dynamic agent technology
JDK Agent: dynamic Agent Technology based on Interface
Cglib proxy: dynamic proxy Technology based on parent Class
two。 Dynamic Agent Code based on jdk
/ /-Interface 1-package com.itspring.proxy.jdk;public interface TargetInterface1 {void save ();} / /-Interface 2-package com.itspring.proxy.jdk;public interface TargetInterface2 {void update () } / /-Interface 1, Interface 2 implementation class (target class)-package com.itspring.proxy.jdk;// target class (enhanced class) public class Target implements TargetInterface1, TargetInterface2 {public void save () {System.out.println ("save running...");} public void update () {System.out.println ("update running...") }} /-Notification Class (method Enhancement Class)-package com.itspring.proxy.jdk;// Notification Class (Enhancement Class) public class Advice {public void before () {System.out.println ("pre-enhancements...");} public void afterRunning () {System.out.println ("Post enhancements...") }} / /-Test code-package com.itspring.proxy.jdk;import org.junit.Test;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;public class ProxyTest {@ Test public void test1 () {final Target target = new Target (); final Advice advice = new Advice () / / the return value is the dynamically generated proxy object TargetInterface1 proxy1 = (TargetInterface1) Proxy.newProxyInstance (target.getClass () .getClassLoader (), / / the class loader target.getClass () .getInterfaces () of the target class) / / the interface implemented by the target class (there may be multiple) new InvocationHandler () {/ / calls any method of the proxy object, essentially calling the invoke method public Object invoke (Object proxy, / / proxy object Method method) / / Target method object Object [] args / / parameters of the target method) throws Throwable {System.out.println ("method in progress:" + method.getName ()) Advice.before (); / / pre-enhanced Object invoke = method.invoke (target, args); / / execute target method advice.afterRunning (); / / Post-enhanced return invoke;}}) Proxy1.save () / / the return value is the dynamically generated proxy object TargetInterface2 proxy2 = (TargetInterface2) Proxy.newProxyInstance (target.getClass () .getClassLoader (), / / the class loader target.getClass () .getInterfaces () of the target class) / / the interface implemented by the target class (there may be multiple) new InvocationHandler () {/ / calls any method of the proxy object, essentially calling the invoke method public Object invoke (Object proxy, / / proxy object Method method) / / Target method object Object [] args / / parameters of the target method) throws Throwable {System.out.println ("method in progress:" + method.getName ()) Advice.before (); Object invoke = method.invoke (target, args); / / execute target method advice.afterRunning (); return invoke;}}); proxy2.update ();}}
3. Dynamic Agent Code based on cglib
Cglib is a third-party library, and spring integrates cglib.
/ /-package com.itspring.proxy.cglib;import com.itspring.proxy.jdk.TargetInterface1;import com.itspring.proxy.jdk.TargetInterface2;// target class (enhanced class) public class Target {public void save () {System.out.println ("save running...") } public void update () {System.out.println ("update running...");}} /-Notification Class (enhanced Class)-package com.itspring.proxy.cglib;// Notification Class (enhanced Class) public class Advice {public void before () {System.out.println ("pre-enhanced...") } public void afterRunning () {System.out.println ("Post Enhancement...");}} / /-Test Code-package com.itspring.proxy.cglib;import com.itspring.proxy.jdk.TargetInterface1;import com.itspring.proxy.jdk.TargetInterface2;import org.junit.Test;import org.springframework.cglib.proxy.Enhancer;import org.springframework.cglib.proxy.MethodInterceptor Import org.springframework.cglib.proxy.MethodProxy;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;public class ProxyTest {@ Test public void test1 () {/ / Target object final Target target = new Target (); / / enhanced object final Advice advice = new Advice (); / / generate dynamic proxy object based on cglib / / 1. Create an intensifier Enhancer enhancer = new Enhancer (); / / 2. Create the parent class enhancer.setSuperclass (Target.class); / / 3. Set callback enhancer.setCallback (new MethodInterceptor () {public Object intercept (Object proxy, / / proxy object Method method, / / target method Object [] objects) / / Parameter MethodProxy methodProxy of the target method) / / proxy throws Throwable {/ / pre-enhanced advice.before () of the target method / / Target method Object invoke = method.invoke (target, objects); / / Post enhanced advice.afterRunning (); return invoke;}}); / / 4. Generate the proxy object Target target1 = (Target) enhancer.create (); / / 5. Test target1.save (); target1.update ();}}
This is the end of the introduction of "what is the concept of AOP and the principles of the two dynamic agent modes in Spring". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.