"Internet Architecture" Software Architecture-Spring AOP of Spring Source Code (10)

>Spring is a technical system. I mentioned spring's ioc before. Learn the meaning of the bottom layer, so that when you flexibly customize it in the future, you can use the learned bottom layer knowledge. I remembered that during development, the log logs were all debug. When it came online, it was changed to info in order not to affect the performance. But if there is a problem online, you need to close the project and change it to debug to see what happened. , In fact, if you learn aop, there is a switch function in it, you can dynamically change the log level without changing the status of the project, and flexibly track the problems in the project.

![](https://upload-images.jianshu.io/upload_images/11223715-10b40dc963d162b3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


#### AOP
* Definition
>When it is necessary to define program logic in batches and change the program in batches, there are actually many pitfalls in it, which make the program itself very complicated. Some people like to increase the cache in AOP, which brings convenience and complexity. When the code becomes more extensible, the complexity becomes very strong. Especially for novices, choose carefully, especially in terms of responsibility and scalability. Don't make the plan too complicated. Use simple solutions to achieve complex problems, rather than complex solutions to achieve simple problems. Develop years of insights.

* Notification interception component
> specific logical carrier to cut into.


* Aop Definition Component
> describes which notifications aop specifically includes and which methods are specifically cut into.

* Call actor
> target object proxy implementation.
1. java proxy
2. cglib proxy

* Other
1. AopProxyBeanFactory
2. AopProxy

>Process combing: First, I will write a logic component that is intercepted by AOP. What is to be intercepted and the specific business. The AOP-defined component tells me which notification and interception components are specific, and which methods to insert it into. Which specific methods are executed by calling the execution component. The corresponding aopproxy is generated by other aopbeanfactroy, and what the user gets is the business service class, and the goal is transparency. The meaning of transparency is that for the user, the caller does not need to care about these, and the intermediate method is not shown to the user through the proxy method, which is transparent. To be honest, AOP is more complicated than IOC.

#### AOP Hu concept

* Connection point (Jointpoint): Indicates the need to insert extension points of cross-cutting concerns in the program. The connection points may be class initialization, method execution, method invocation, field invocation, or exception handling, etc. Spring only supports method execution connection points. In AOP, it means "where to do it";

* Pointcut: Select a set of related connection point patterns, that is, a collection of connection points. Spring supports perl5 regular expressions and AspectJ pointcut patterns. Spring uses AspectJ syntax by default, which is expressed as "where" in AOP Dry set";

* Advice: The behavior performed on the connection point. The notification provides a means to extend the existing behavior at the connection point selected by the entry point in AOP; including before advice and post advice (after advice) and around advice. In Spring, AOP is implemented through the proxy mode, and notifications are woven into the interceptor chain around the connection point through the interceptor mode; it is expressed as "what to do" in AOP;

* Aspect/Aspect: Modularization of cross-cutting concerns, such as the log component mentioned above. It can be regarded as a combination of notification, introduction, and entry point; in Spring, Schema and @AspectJ can be used for organization and realization; in AOP, it is expressed as "where and what collection";

* Introduction (inter-type declaration): also known as internal type declaration, adding additional new fields or methods to existing classes, Spring allows the introduction of new interfaces (must correspond to an implementation) to all proxied objects (target objects) , Expressed as "what to do (what to introduce)" in AOP;

* Target Object: The object that needs to be woven into the crosscutting focus, that is, the object is the object selected by the entry point, and the object that needs to be notified, which can also be called the "notified object"; because Spring AOP passed The proxy mode is implemented, so that this object is always a proxy object, which is expressed as "to whom" in AOP;

* AOP proxy (AOP Proxy): AOP framework uses objects created by proxy mode to insert notifications (ie application aspects) at the connection point, which is to apply aspects to the target object through the proxy. In Spring, AOP proxy can be implemented with JDK dynamic proxy or CGLIB proxy, and the aspect is applied through the interceptor model.

* Weaving: Weaving is a process of applying aspects to target objects to create AOP proxy objects. Weaving can be done during compilation, class loading, and runtime.
In AOP, the connection point of the target object is selected through the entry point, and then the notification is woven at the corresponding connection point of the target object. The entry point and the notification are the aspect (cross-cutting concerns), and the aspect is applied at the connection point of the target object The implementation is through AOP proxy objects.

![](https://upload-images.jianshu.io/upload_images/11223715-c508e5c6af792184.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


PS: AOP currently uses several scenarios, such as log management, transaction control, and authority management.
 

Guess you like

Origin blog.csdn.net/zhugeaming2018/article/details/109110855