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: register advice programmatically at runtime

The previous solution is too invasive as it not only creates advice on the fly but also handles advising beans. This replicates functionality of Spring’s AbstractAdvisorAutoProxyCreator, specifically the getAdvicesAndAdvisorsForBean method, where Spring will locate and apply eligible Advisors to each bean. A better approach is to simply programmatically create Advisors and let Spring handle the … Read more

Configuring AspectJ aspects using Spring IoC with JavaConfig?

Turns out that there is an org.aspectj.lang.Aspects class to provide for specifically this purpose. It appears that the aspectOf() method is added by the LTW which is why it works fine in XML configuration, but not at compile time. To get around this limitation, org.aspectj.lang.Aspects provides a aspectOf() method: @Bean public com.xyz.profiler.Profiler profiler() { com.xyz.profiler.Profiler … Read more

How to instrument / advice a Spring Data (JPA) repository?

Although the OP heavily relied on AspectJ solutions, the question as it stands doesn’t directly suggest that solutions should be limited to AspectJ. Therefore I’d like to offer a non-AspectJ way to advise a Spring Data JPA Repository. It is based upon adding a custom Interceptor into barebone Spring AOP proxy interceptor chain. First, configure … Read more

Spring + AspectJ weaving for java 8 using aspectj-maven-plugin

Solution before the official release prior to Sep 2015 After many headaches and many hours struggling against this, fortunately I could solve this problem. Here is what I did: To use aspectj-maven-plugin with Java 8 I could configure version aspectj-maven-plugin 1.7 (Note that aspectj-maven-plugin 1.6 works for Java 7). So, the maven plugin configuration needs … Read more

logging with AOP in spring?

Spring makes it really easy for us to make use of AOP. Here’s a simple logging example: @Aspect public class MyLogger { private Logger log = Logger.getLogger(getClass()); @After(“execution(* com.example.web.HomeController.*(..))”) public void log(JoinPoint point) { log.info(point.getSignature().getName() + ” called…”); } } Then simply configure your applicationContext.xml (or equivalent): <aop:aspectj-autoproxy> <aop:include name=”myLogger”/> </aop:aspectj-autoproxy> <bean id=”myLogger” class=”com.example.aspect.MyLogger”/> You’ll … Read more

Spring AOP Advice on Annotated Controllers

It’s possible to have advice on annotated controllers. I assume you want to advice after execution of all methods in classes annotated with @Controller. Here’s an example: import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspect public class ControllerAspect { @Pointcut(“within(@org.springframework.stereotype.Controller *)”) public void controllerBean() {} @Pointcut(“execution(* *(..))”) public void methodPointcut() {} @AfterReturning(“controllerBean() && methodPointcut() “) public … Read more