【问题标题】:Why Spring Boot AOP pointcut not triggered为什么没有触发 Spring Boot AOP 切入点
【发布时间】:2019-09-15 13:46:52
【问题描述】:

wanner 用最少的代码测试 spring boot(1.5.20) aop

类被优化,

 @Component
 public class Test {
     public Test() {
         System.out.println("test constr");
     }

     public void print() {
         System.out.println("test print");
     }
 }

aop 类

@Aspect
@Component
public class LoggingAspect {
    public LoggingAspect() {
        System.out.println("aspect constr");
    }

    @After("execution(* *.Test.*(..))")
    public void log(JoinPoint joinPoint) {
        System.out.println("aspect print");
    }
}

主类

@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)

public class AopApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(AopApplication.class, args);
    }

    @Autowired
    private Test test;

    @Override
        public void run(String... strings) throws Exception {
        test.print();
    }
}

Test bean 和 LoggingAspect bean 都已创建。 Test.pring 被执行。但是,切入点 log() 永远不会被触发。我如此搜索并没有找到答案。我还尝试了使用 proxyTargetClass = True 或 False 的 @EnableAspectJAutoProxy。据我了解,此参数强制将 cglib 用于测试类。

请告诉我我错过了什么

【问题讨论】:

    标签: spring-boot spring-aop


    【解决方案1】:

    弄清楚。从 .Test. 更改为 com.example.aop.Test.*,然后工作。

    【讨论】:

    • 如果你想要更通用的包名,你也可以使用*..Test。您的问题是 *.Test 假定该类直接在顶级包中,例如com.Test.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    相关资源
    最近更新 更多