Deep understanding of spring AOP
I preface
In previous projects, I seldom paid attention to the specific implementation and theory of spring AOP. I just had a brief understanding of what AOP is and how to use it. After seeing a blog post that is well written, I'll learn it.
AOP
AOP (aspect oriented programming), that is, aspect oriented programming, can be said to be OOP (object oriented programming). OOP introduces the concepts of encapsulation, inheritance and polymorphism to establish an object hierarchy, which is used to simulate a collection of public behaviors. However, OOP allows developers to define vertical relationships, but it is not suitable to define horizontal relationships, such as logging functions. Logging code is often distributed horizontally in all pairs Like in the hierarchy, it has nothing to do with the core functions of its corresponding objects. This is also true for other types of code, such as security, exception handling and transparent persistence. This irrelevant code scattered everywhere is called cross cutting. In OOP design, it leads to a large number of code duplication, which is not conducive to the reuse of various modules.
On the contrary, AOP technology uses a technology called "crosscutting" to dissect the interior of encapsulated objects, encapsulate the public behaviors that affect multiple classes into a reusable module, and name it "aspect", that is, aspect. The so-called "aspect" simply means that the logic or responsibilities that have nothing to do with the business but are jointly invoked by the business modules are encapsulated, which is convenient to reduce the repeated code of the system, reduce the coupling degree between modules, and facilitate the operability and maintainability in the future.
Using "crosscutting" technology, AOP divides the software system into two parts: core concerns and crosscutting concerns. The main process of business processing is the core concern, and the part that has little to do with it is the crosscutting concern. A characteristic of crosscutting concerns is that they often occur in multiple parts of the core concerns, and they are basically similar everywhere, such as authority authentication, logs and things. The role of AOP is to separate various concerns in the system and separate the core concerns from the crosscutting concerns.
AOP core concepts
1. Crosscutting concerns
The concerns about which methods to intercept and how to deal with them after interception are called crosscutting concerns
2. Aspect
Class is the abstraction of object features, and facet is the abstraction of crosscutting concerns
3. Join point
The intercepted point. Because spring only supports connection points of method types, connection points in spring refer to the intercepted methods. In fact, connection points can also be fields or constructors
4. Pointcut
Definition of interception of connection points
5. Advice
The so-called notification refers to the code to be executed after intercepting the connection point. Notifications are divided into five categories: pre, post, exception, final and surround notifications
6. Target object
Target object of proxy
7. Weave
The process of applying facets to the target object and causing the proxy object to be created
8. Introduction
Without modifying the code, you can dynamically add some methods or fields to the class at run time
Spring support for AOP
The spring IOC container is responsible for generating and managing the AOP proxy in spring, and its dependencies are also managed by the IOC container. Therefore, the AOP proxy can directly use other bean instances in the container as targets, and this relationship can be provided by the dependency injection of the IOC container. The rules for spring to create proxy are:
1. By default, the Java dynamic proxy is used to create the AOP proxy, so that the proxy can be created for any interface instance
2. When the class to be proxy is not a proxy interface, spring will switch to using cglib proxy, or force cglib to be used
AOP programming is actually a very simple thing. Looking at AOP programming, programmers only need to participate in three parts:
1. Define common business components
2. Define pointcuts. One pointcut may crosscut multiple business components
3. Define enhancement processing, which is the processing action woven for ordinary business components in the AOP framework
Therefore, the key to AOP programming is to define the entry point and define the enhancement processing. Once the appropriate entry point and enhancement processing are defined, the AOP framework will automatically generate the AOP proxy, that is, the method of the proxy object = enhancement processing + the method of the proxy object.
The following is an example of spring AOP XML file template, called AOP XML, and the subsequent contents are in AOP Extend on XML:
Simple implementation of AOP based on spring
Please note that before the explanation, it should be noted that to successfully run the code using spring AOP, only the jar package provided by spring to developers is not enough. Please download two additional jar packages online:
1、aopalliance. jar
2、aspectjweaver. jar
To explain the XML implementation of spring AOP, first define an interface:
Define two interface implementation classes:
Crosscutting concerns, here is the printing time:
With these three classes, you can implement a simple spring AOP. Take a look at AOP XML configuration:
Write a main function to call:
The operation result is:
You can see that all the methods of the two implementation classes of the HelloWorld interface are added with proxies, and the proxy content is the print time
Spring based AOP uses other details
1. Add a crosscutting concern and print the log. The Java class is:
The test class remains unchanged, and the print result is:
There are two ways to use loghandler before timehandler:
(1) There is an order attribute in the aspect. The number of the order attribute is the order of crosscutting concerns
(2) Define loghandler in front of timehandler. Spring takes the definition order of aspects as the weaving order by default
2. I just want to weave some methods into the interface
Just modify the expression of pointcut:
It means that timehandler will only weave into the methods beginning with print of HelloWorld interface, and loghandler will only weave into the methods beginning with do of HelloWorld interface
3. Force cglib to generate proxy
As mentioned earlier, there are rules for spring to generate proxy using dynamic proxy or cglib. Higher versions of spring will automatically choose whether to use dynamic proxy or cglib to generate proxy content. Of course, we can also force cglib to generate proxy, that is, there is a "proxy target class" attribute in < AOP: config >, and if this attribute value is set to true, Then the class based proxy will work. If the proxy target class is set to false or this attribute is omitted, the interface based proxy will work
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.