Spring - AspectJ pointcut for constructor object with annotation

I am developing a java application (JDK1.6) with the Spring framework (4.0.5) and AspectJ for registering AOP.

My Aspect classes work fine, but I cannot create a pointcut for the constructor object.

This is my object:

@Controller public class ApplicationController { public ApplicationController(String myString, MyObject myObject) { ... } ... .. . } 

This is my Aspect class:

 @Aspect @Component public class CommonLogAspect implements ILogAspect { Logger log = Logger.getLogger(CommonLogAspect.class); // @Before("execution(my.package.Class.new(..))) @Before("execution(* *.new(..))") public void constructorAnnotatedWithInject() { log.info("CONSTRUCTOR"); } } 

How to create a pointcut for my constructor object?


thanks

+5
source share
1 answer

Sotirios Delimanolis is right, because Spring AOP does not support constructor capture, you need the full AspectJ to do this. Spring Guide, chapter 9.8 Using AspectJ with Spring Applications describes how to use it with LTW (boot time).

In addition, there is a problem with your pointcut

 @Before("execution(* *.new(..))") 

Constructors do not have return types, such as methods in AspectJ syntax, so you need to remove the leading * :

 @Before("execution(*.new(..))") 
+9
source

Source: https://habr.com/ru/post/1209361/


All Articles