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?
Give this a try.
@Around("@annotation(javax.annotation.PostConstruct)")
public void myAdvice(ProceedingJoinPoint jp) throws Throwable{
System.out.println("This is before " + jp.getSignature().getName() + "()");
jp.proceed();
}
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
After that you should see in the Sysout:
This is before test()
test() is executed
Enjoy!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With