Spring - AspectJ pointcut for constructor object with annotation

Matteo Codogno picture Matteo Codogno · Dec 17, 2014 · Viewed 9k times · Source

I'm developing a java (JDK1.6) application with Spring framework(4.0.5) and AspectJ for AOP Logging.

My Aspect classes work fine but I can't create a pointcut for 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 can I create a pointcut for my constructor object?


Thanks

Answer

kriegaex picture kriegaex · Dec 17, 2014

Sotirios Delimanolis is right insofar as Spring AOP does not support constructor interception, you do need full AspectJ for it. The Spring manual, chapter 9.8 Using AspectJ with Spring applications, describes how to use it with LTW (load-time weaving).

Furthermore, there is a problem with your pointcut

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

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

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