Spring Boot 使用AOP

Spring Boot 使用AOP

  1. 在pom文件中添加AOP依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
  1. 添加切入点类
@Component
@Aspect
public class LogAop {
    @Before("execution(*  com.aimilin.demo..*.*(..))")
    public void before() {
        System.out.println("start ---- log");
    }

    @After("execution(* com.aimilin.demo..*.*(..))")
    public void after(JoinPoint joinPoint) {
        System.out.println(
                "after --------log, Class: " + joinPoint.getTarget().getClass() + "\n" + 
                "Method: " + joinPoint.getSignature().getName() + "\n" + 
                "Args :" + Arrays.asList(joinPoint.getArgs()));
    }
}
  1. Spring Boot Aop常用配置
    配置文件位置: AopAutoConfiguration
# 是否启用AOP,默认启用
spring.aop.auto=true 

# true 使用JDK代理(类需要有接口),false - cgLib代理
spring.aop.proxy-target-class=true
    4.

猜你喜欢

转载自blog.csdn.net/afgasdg/article/details/79758601