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

查看:105
本文介绍了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的演示项目.克隆 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天全站免登陆