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
You can use boolean operators: expression="execution(* package1. *. *(..))
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).
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? 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.
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(..))")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With