为了账号安全,请及时绑定邮箱和手机立即绑定

Spring AOP 没有错误但不执行通知

Spring AOP 没有错误但不执行通知

qq_遁去的一_1 2021-06-11 19:42:09
我是 Spring AOP 的新手,并按照baeldung文章尝试了以下代码。方面类:@Aspect@Componentpublic class LoggingAspect {    private final static Logger LOGGER = LoggerFactory.getLogger(LoggingAspect.class);    @Around("@annotation(Debug)")    public Object beforeDebug(ProceedingJoinPoint debugJoinpoint) {        LOGGER.debug("----------------Debug message logged from {}", debugJoinpoint.getSignature().toString());        System.out.println("IN HERE");        long start = System.currentTimeMillis();        Object proceed = null;        try {            proceed = debugJoinpoint.proceed();        } catch (Throwable e) {            e.printStackTrace();        }        long executionTime = System.currentTimeMillis() - start;        System.out.println(debugJoinpoint.getSignature() + " executed in " + executionTime + "ms");        return proceed;    }}注解:@Retention(RUNTIME)@Target(METHOD)@Loggable(type = "debug")public @interface Debug {}用于测试的演示类:@SpringBootApplication@Componentpublic class Application {    public static void main(String[] args) throws InterruptedException {        SpringApplication.run(Application.class, args);        dumb();    }    @Debug    public static void dumb() throws InterruptedException {        Thread.sleep(2000);    }}有人可以指出我在上面做错了什么吗?该建议不会执行,而且我在这里和那里谷歌搜索后无法解决这个问题,不知道我错过了什么。
查看完整描述

1 回答

?
守着星空守着你

TA贡献1799条经验 获得超8个赞

Spring AOP 仅适用于非静态方法,如 Baeldung 示例中所示并记录在 Spring 手册中。


查看完整回答
反对 回复 2021-06-23
  • 1 回答
  • 0 关注
  • 242 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信