Spring learning journey (6) working principles of Spring AOP: springaop
AOP (Aspect-Oriented Programming) is one of the key technologies provided by Spring.
Based on IoC, AOP is an effective supplement to OOP (Object-Oriented Programming, object-oriented Programming.
Using a "cross-cutting" technique, you can segment the encapsulated objects and encapsulate the public behaviors that affect multiple classes into a reusable module, and name it Aspect (Aspect, also known as Aspect ).
The core idea of AOP is to "separate the business logic in an application from the General Service functions supported by the application, and integrate the business logic with the General Service functions through configuration information ".
The technology for Implementing AOP is mainly divided into two categories:
First, the dynamic proxy technology is used to intercept a message by using an interceptor, so as to replace the execution of the original object behavior;
The second is to use static weaving to introduce specific syntax to create "cut-plane", so that the compiler can weave the "cut-plane" code during compilation.
Spring AOP is a technology for Implementing AOP and is implemented using "dynamic proxy technology.
(1) concepts and terminologies related to AOP technology
1) several related concepts:
In the previous post of the small series, when processing the original business logic and adding the AOP part, the execution of all methods in Class A has become A "Focus ", this is called "cross-cutting concerns ". In this way, the original business logic is separated, so this point becomes the connection point of the logic before and after the connection, known as the "connection point ". Then, for the original business logic, we need to tell the program "when to add new additional features". Here, "when" is called "notification ". In this example, there are two "notifications": "Add before all methods in Class A" is called "pre-notification "; "Add after execution of all methods in Class A" is called "post notification ".
In this example, the frontend notification is as follows:
<! -- Configure the start point of the pre-notification and pre-notification --> <aop: before method = "methodX1" pointcut = "execution (* com.edu. aop. hello. a. * () "> </aop: before>
Post notification:
<! -- Configure the entry point for post-notification and post-notification --> <aop: after method = "methodX2" pointcut = "execution (* com.edu. aop. hello. a. * () "> </aop: after>
When the original business logic is divided by "Horizontal Focus", you can insert (cut in) the new requirement function module at the "point", which will specify the insert (cut in) the "place" (Configuration File Code) of the new functional module is called "Pointcut", and the functional module to be inserted is called "Aspect )". It can be seen that configuring the "aspect" means to specify the place (connection point) to which the module (aspect) to be switched.
2) Aspect (Aspect, also called Aspect)
It is an AOP conceptual entity (that is, defining a class) That modularize the logic of concerns in the system. It can contain multiple Pointcut and related Advice (notification) definitions. For example, Class X in this example.
3) JoinPoint)
Before running the system, the function module of AOP needs to be woven into the function module of OOP. To perform this weaving process, you need to know which functional points of the system to perform the weaving operation. The system functional points on which to perform the weaving operation are called JoinPoint.
In Spring AOP, a connection point always indicates the execution of a method.
Common JoinPoint types: method call, method execution, constructor call, constructor execution, field setting, Field Retrieval, exception handling execution, and class initialization.
4) Pointcut)
Pointcut is a description of JointPoint, which is used to indicate which Joinpoint to intercept.
(Beginners can understand that the two are the same, except that Pointcut is used in the notification of the configuration file; JoinPoint is used in the partition class)
Pointcut is usually associated with "A Pointcut expression" to indicate the functional points on which AOP needs to be woven.
Common Pointcut methods:
(1) directly specify the method name of the JoinPoint;
(2) Regular Expressions. Spring AOP supports this method;
(3) Use a specific Pointcut Expression Language.
5) Advice (notification)
An Advice corresponds to the logic of a cross-concern. It represents the cross-concern logic that will be woven into a JointPoint: the logic that is executed on a specific connection point of a plane.
One aspect includes one or more notifications, each of which is to tell "at what time" what method is called.
Spring AOP provides five types of notifications:
Before Advice (pre-notification), After Returning Advice (return notification), After throwing Advice (exception notification), After Advice (post notification), and Around Advice (surround notification)
6) Target (Target object)
The objects to which the notification is applied (objects that contain connection points, or objects that are notified or proxied) meet the conditions specified by Pointcut, objects of the cross-cutting logic will be woven during the weaving process.
In this example, all methods of Class A are used.
7) AOP Proxy (AOP Proxy)
The object created by the AOP framework to execute the content of the Aspect (Aspect.
8) Weave (woven)
The process of applying Aspect to the Target object and causing Proxy object creation is called weaving.
9) Interceptor)
Interceptor (also called interceptor) is another method of AOP.
(2) Entry Point expression
Generally, the Pointcut expression is used to describe the JointPoint format and indicate which joinpoints should be intercepted.
The format of the entry point expression is as follows:
Execution (method access permission: return the package path of the type method. Method Name (method parameter) exception type)
For example, in this example:
execution(* com.edu.aop.hello.A.*())
Instructions for use:
1) "return type", "method name", and "method parameter" are required. Other items are optional;
2) wildcard characters can be used in the "entry point expression.
Common wildcard characters:
"*" Indicates a string that does not contain ".". To include ".", you must use the escape symbol "\.".
".." Indicates any number of parameters of any type.
3) The method name matches the name pattern. You can use the * wildcard to match all method names.
4) if the package is in the same package, you can omit the package name and directly give the class (Interface) Name;
5) the start point expression can be expressed through the operators "&", "|", and "!". To form a new entry point expression.
Example:
execution(* com.edu.aop.hello.A.*())||execution(* *.add(..))
Reference books: Java EE Framework development technology and Case Study