The advantage of AOP is that you just do your job and someone else does the rest for you. Maybe one day, you want to run naked and not wear clothes, so you just fire servant A. Perhaps one day you will want to take some money with you before you go out, so you will hire another servant to help you draw it out. This is AOP, where each person does his or her job and combines it flexibly to achieve a configurable, pluggable program structure.
Why do WE need to program faceted?
The benefits of object-oriented programming (OOP) are obvious, but so are the drawbacks. When the need for multiple inheritance does not have the method of adding a common object, such as logging, performance monitoring, etc., using object-oriented programming method, need to add the same way in each object, the result is larger repeat workload and a large number of repeat code, is not conducive to maintain. Aspect oriented programming (AOP) is a supplement to object oriented programming. Simply speaking, AOP is a programming idea that uniformly deals with a certain aspect problem. If you use AOP for logging and processing, all the logging code is centralized in one place, no need to add in each method, greatly reducing the duplication of code.
Spring AOP terminology
Advice contains crosscutting behavior that needs to be used by multiple application objects. It’s ok to be completely unintelligible, but in layman’s terms it defines “when” and “what to do.”
Join points are all points during program execution where advice can be applied.
The Poincut defines “where” the cut should be made and which join points should be notified. Obviously, the cut point must be the join point.
An Aspect is a combination of notification and pointcut. Together, notifications and pointcuts define the entirety of the aspect — what, when, and where functionality is accomplished.
Introduction allows you to add new methods or attributes to an existing class.
Three, the AOP combat
To use AOP, you need to add spring-boot-starter-AOP dependencies as follows:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
Copy the code
You don’t need to add aspectJWeaver’s dependencies anymore because spring-boot-starter-AOP includes AspectJWeaver and is a newer version that will cause an error if you start adding an older version (such as 1.5.4).
3.2 Write an intercepting bean to define a controller directly as follows:
@RestController public class AopController {
@RequestMapping("/hello")
public String sayHello(){
System.out.println("hello");
return "hello"; }}Copy the code
Spring annotates POJOs with the @AspectJ annotation, which indicates that the class is not just a POJO, but an aspect. An aspect is a combination of pointcuts and advice, so defining an aspect requires writing pointcuts and advice. In the code, just add the @aspect annotation.
Pointcuts are defined using the @pointcut annotation and Pointcut expressions.
The @pointcut annotation defines reusable pointcuts within a section.
Because Spring’s granularity is minimal to the method level, execution expressions can be used to explicitly specify method return types, class names, method names, and parameter names, and in practice, most business scenarios that require AOP only need to reach the method level. Thus the execution expression is most widely used. Here is the syntax for an execution expression:
Execution means triggered when a method is executed. Starting with “” indicates that the method return value type is arbitrary. The fully qualified class and method names are followed by “”, which can represent any class and any method. For method parameter lists, use “…” Indicates that the parameter is of any type. If you need more than one expression, can use “&”, “| |” and “!” Complete the and, or, and no operations.
3.3.2 Definition of Notifications There are five types of notifications:
Pre-notification (@before) : The notification is called Before the target method is called
Post-notification (@after) : The notification is invoked After the target method completes
Around notification (@around) : Executes a custom method before and after a notified method call
Return notification (@afterRETURNING) : Notification is called after successful execution of the target method
Exception Notification (@AfterThrowing) : The notification is called after the target method throws an exception
@Aspect @Component public class AopAdvice {
@Pointcut("execution (* com.shangguan.aop.controller.*.*(..) )")
public void test() {
}
@Before("test()")
public void beforeAdvice() {
System.out.println("beforeAdvice...");
}
@After("test()")
public void afterAdvice() {
System.out.println("afterAdvice...");
}
@Around("test()")
public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {
System.out.println("before");
try {
proceedingJoinPoint.proceed();
} catch (Throwable t) {
t.printStackTrace();
}
System.out.println("after"); }}Copy the code
3.4 After the completion of the startup test, the code structure is shown in the figure:
Run AopApplication, visit http://localhost:8080/hello in your browser, not surprisingly, the console output as shown: