This article mainly introduces the role of AOP under the Spring framework of Java. Friends who need it can refer to it
If there is no aop, when doing log processing, we will add logs to each method. Processing, such as
# But most days the processing code is the same. In order to achieve code reuse, we may separate the log processing into a new method. But then we still have to insert these methods manually.
But these two methods are strongly coupled. If we don’t need this function at this time, or want to switch to other functions, then we must one by one Revise.
Through dynamic proxy, the corresponding process can be executed at the specified location. In this way, some horizontal functions can be extracted to form an independent module, and then these functions can be inserted into the specified location
. This kind of thinking is called aspect-oriented programming, or AOP.
In order to perform these horizontal functions at the specified location, you need to know where the specified location is
For example, in the figure above, the method-level aop implementation, in a program execution chain, calls methodDetailed explanation of the role of Spring Aop in JAVA the cut point, which means that the cross-cutting function will be executed when methodDetailed explanation of the role of Spring Aop in JAVA is executed, then whether it is before or after methodDetailed explanation of the role of Spring Aop in JAVA? What is the execution? These are specified by advice. There are Detailed explanation of the role of Spring Aop in JAVA types of advice, namely
Advice type Introduction
Before (preadvice) Executed before the target method is called
After (post notification) Executed after the target method is called
After-returning (return notification) Executed after the target method is successfully executed
After-throwing (Exception notification) After the target method throws an exception, execute
Around (around notification), which is equivalent to merging the prefix and postfix
to combine the pointcut and notification. Together they are aspects. An aspect specifies when and where to execute which method. Define this aspect in spring aop like this:
@Aspect @Component public class UserAspect { @Before("execution(* com.aop.service.impl.UserServiceImpl.login(..))") public void loginLog(){ System.out.println("user login"); } }
Use the annotation @Aspect to declare a specific class as an aspect. In this way, the methods under the class can be declared as horizontal function points and inserted into the specified position. . Use execution expression to declare at this pointcut, the format is as follows
#The first position specifies the return value of the method, the * sign represents any type of return value, and then the location In the class and method names, the * sign also represents any, which is any method in the class. In the previous example, the method name is login, which specifies the login method in the class. Then the last parameter is the method input parameter. Because Java supports overloading, this parameter can help you position it more accurately. Two dots represent any parameter type. In this way, the execution expression tells the program where to execute the notification. The method modified by annotations such as @Before is the content of the notification, that is, what to do.
At this point, we can use spring aop, but there are still two points that need to be paid attention to
Declare the aspect class as a bean
Method specified by the pointcut The class it belongs to also needs to be injected by spring to take effect
The above is the detailed content of Detailed explanation of the role of Spring Aop in JAVA. For more information, please follow other related articles on the PHP Chinese website!