简体   繁体   中英

Spring - AspectJ pointcut for constructor object with annotation

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

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(..))")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM