Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 791
Matteo Codogno Avatar asked Dec 17 '14 14:12

Matteo Codogno


People also ask

How do you specify a single pointcut for multiple packages?

You can use boolean operators: expression="execution(* package1. *. *(..))

What is AspectJ and annotation?

The use of the @AspectJ annotations means that there are large classes of AspectJ applications that can be compiled by a regular Java 5 compiler, and subsequently woven by the AspectJ weaver (for example, as an additional build stage, or as late as class load-time).

What is pointcut annotation?

Pointcut is a set of one or more JoinPoint where an advice should be executed. You can specify Pointcuts using expressions or patterns as we will see in our AOP examples. In Spring, Pointcut helps to use specific JoinPoints to apply the advice.

Which tag informs the Spring container about the use of AspectJ annotation?

Which tag informs the spring container about the use of AspectJ annotation? Explanation: To enable AspectJ annotation support in the Spring IoC container, you only have to define an empty XML element aop:aspectj-autoproxy in your bean configuration file.


1 Answers

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(..))")
like image 127
kriegaex Avatar answered Sep 27 '22 23:09

kriegaex