The use of aop in springboot

      When I joined a new project team in the company, I used a lot of annotated aop interceptors. In the past, my learning was limited to theory. I found that it was a deeper feeling when I used it in practical applications. The biggest feeling is convenience and simplicity. Knowing the application methods in the company, I decided to accumulate it, but I felt that my writing was limited, so I checked and learned online, and found an easy-to-learn article, which I reprinted for my own study. This article is limited to entry. If you want to have the same feeling as the landlord, it is recommended to apply it in the actual project, you will feel the magic!

The article part is as follows:

First add maven dependencies to your SpringBoot project to support aop (in fact, some jars required by aop are automatically introduced)

Add dependencies in pom.xml:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

Then create the Aspect test class:

package com.shanhy.sboot.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect // FOR AOP
@Order(-99) // 控制多个Aspect的执行顺序,越小越先执行
@Component
public class TestAspect {

    @Before("@annotation(test)")// 拦截被TestAnnotation注解的方法;如果你需要拦截指定package指定规则名称的方法,可以使用表达式execution(...),具体百度一下资料一大堆
    public void beforeTest(JoinPoint point, TestAnnotation test) throws Throwable {
        System.out.println("beforeTest:" + test.name());
    }

    @After("@annotation(test)")
    public void afterTest(JoinPoint point, TestAnnotation test) {
        System.out.println("afterTest:" + test.name());
    }

}

This is done, then create a Controller to verify:

@RestController
@RequestMapping("/test")
public class TestController {

    @TestAnnotation(name="abc")
    @RequestMapping("/show")
    public String show() {
        return "OK";
    }

    @RequestMapping("/show2")
    public String show2() {
        return "OK2";
    }
}

At this point, when we access the show request, it will be intercepted and the console will print output. If show2 is requested it will not be intercepted.

Note: 
1. There is no need to add spring.aop.auto=true in application.properties, because this is true by default, and the value of true is to enable the @EnableAspectJAutoProxy annotation. 
2. You do not need to manually add the @EnableAspectJAutoProxy annotation. 
3. When you need to use CGLIB to implement AOP, you need to configure spring.aop.proxy-target-class=true, the default value is false, otherwise the standard Java implementation is used by default.

In fact, the interceptor of aspectj will be parsed into advice in AOP, and finally adapted to MethodInterceptor, which are automatically completed by Spring



The above is a partial reprint. The original address is as follows: https://blog.csdn.net/catoop/article/details/71541612

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325745513&siteId=291194637