10

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

Matteo Codogno
  • 1,569
  • 6
  • 21
  • 36

1 Answers1

19

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(..))")
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • That's exactly what I needed - I assumed that constructor calls would be picked up by a statement like the first one above. – josh-cain Feb 24 '15 at 13:38