Integration tests for AspectJ

Let us use the same sample code as in my answer to the related AspectJ unit testing question: Java class to be targeted by aspect: package de.scrum_master.app; public class Application { public void doSomething(int number) { System.out.println(“Doing something with number ” + number); } } Aspect under test: package de.scrum_master.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import … Read more

Spring: Standard Logging aspect (interceptor)

Yes there are! <bean id=”customizableTraceInterceptor” class=”org.springframework.aop.interceptor.CustomizableTraceInterceptor”> <property name=”enterMessage” value=”Entering $[methodName]($[arguments])”/> <property name=”exitMessage” value=”Leaving $[methodName](): $[returnValue]”/> </bean> <aop:config> <aop:advisor advice-ref=”customizableTraceInterceptor” pointcut=”execution(public * BankAccountServlet.*(..))”/> </aop:config> Check out the CustomizableTraceInterceptor API, you can define separate enter/exit/exception messages with several placeholders: $[methodName] – replaced with the name of the method being invoked $[targetClassName] – replaced with the name of … Read more

Use of proxies in Spring AOP

Spring AOP uses either JDK dynamic proxies or CGLIB to create the proxies for your target objects. According to Spring documentation, in case your target implements at least one interface, a JDK dynamic proxy will be used. However if your target object does not implement any interfaces then a CGLIB proxy will be created. This … Read more

execution Vs. call Join point

Acutally the explanation is quite simple if you understand the basic difference between call() and execution() pointcuts: While the former intercepts all callers (i.e. the sources of method calls), the latter intercepts the calls themselves no matter where they originate from. So how can the number of interceptions triggered by both pointcuts differ? If you … Read more

How to make Lombok and AspectJ work together?

The current answer according to AspectJ maintainer Andy Clement is that there are problems due to ECJ (Eclipse Compiler for Java) packages being included and renamed in the AspectJ compiler infrastructure. For more information there is ongoing discussion between Eric B. and A. Clement on the AspectJ users mailing list: Discussion thread Discussion thread continued … Read more

How to use AOP with AspectJ for logging?

I have created a simple aspect to capture the execution of public methods. The core of this AspectJ code is the pointcut definition: pointcut publicMethodExecuted(): execution(public * *(..)); Here we are capturing all public methods with any return type, on any package and any class, with any number of parameters. The advice execution could be … Read more

@AspectJ pointcut for all methods of a class with specific annotation

You should combine a type pointcut with a method pointcut. These pointcuts will do the work to find all public methods inside a class marked with an @Monitor annotation: @Pointcut(“within(@org.rejeev.Monitor *)”) public void beanAnnotatedWithMonitor() {} @Pointcut(“execution(public * *(..))”) public void publicMethod() {} @Pointcut(“publicMethod() && beanAnnotatedWithMonitor()”) public void publicMethodInsideAClassMarkedWithAtMonitor() {} Advice the last pointcut that combines … Read more

Spring AOP vs AspectJ

Spring-AOP Pros It is simpler to use than AspectJ, since you don’t have to use LTW (load-time weaving) or the AspectJ compiler. It uses the Proxy pattern and the Decorator pattern Spring-AOP Cons This is proxy-based AOP, so basically you can only use method-execution joinpoints. Aspects aren’t applied when calling another method within the same … Read more

Emulate annotation inheritance for interfaces and methods with AspectJ

The problem here is not AspectJ but the JVM. In Java, annotations on interfaces, methods or other annotations are never inherited by implementing classes, overriding methods or classes using annotated annotations. Annotation inheritance only works from classes to subclasses, but only if the annotation type used in the superclass bears the meta annotation @Inherited, see … Read more