Spring AOP 和后构造 [英] Spring AOP and Post Construct

查看:25
本文介绍了Spring AOP 和后构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写下与@PostConstruct 一起使用的方法的名称.但是我发现 AOP 无法绕过" PostConstruct 方法.有没有什么方法可以将 AOP 与 PostConstruct 方法一起使用?

I want to write the name of method which is using with @PostConstruct. But I found that AOP is unable to "Around" the PostConstruct method. Is there any way to use AOP with PostConstruct method?

推荐答案

试试这个.

    @Around("@annotation(javax.annotation.PostConstruct)")
    public void myAdvice(ProceedingJoinPoint jp) throws Throwable{
        System.out.println("This is before " + jp.getSignature().getName() + "()");
        jp.proceed();
    }

此外,您需要激活编译时编织.我在 github 上发布了一个使用 maven 的 Demo 项目.克隆https://github.com/jannikweichert/PostConstructAOPDemo 并执行

Additionally you need to activate compile-time weaving. I published a Demo project on github which uses maven. Clone https://github.com/jannikweichert/PostConstructAOPDemo and execute

mvn clean compile spring-boot:run

之后,您应该会在 Sysout 中看到:

After that you should see in the Sysout:

This is before test()
test() is executed

享受吧!

这篇关于Spring AOP 和后构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆