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

我是 Spring AOP 的新手,并按照baeldung文章尝试了以下代码。


方面类:


@Aspect

@Component

public 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

@Component

public 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);

    }

}

有人可以指出我在上面做错了什么吗?该建议不会执行,而且我在这里和那里谷歌搜索后无法解决这个问题,不知道我错过了什么。


qq_遁去的一_1
浏览 243回答 1
1回答

守着星空守着你

Spring AOP 仅适用于非静态方法,如 Baeldung 示例中所示并记录在 Spring 手册中。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java