Spring autowired bean for @Aspect aspect is null

The aspect is a singleton object and is created outside the Spring container. A solution with XML configuration is to use Spring’s factory method to retrieve the aspect. <bean id=”syncLoggingAspect” class=”uk.co.demo.SyncLoggingAspect” factory-method=”aspectOf” /> With this configuration the aspect will be treated as any other Spring bean and the autowiring will work as normal. You have … 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

Spring – @Transactional – What happens in background?

This is a big topic. The Spring reference doc devotes multiple chapters to it. I recommend reading the ones on Aspect-Oriented Programming and Transactions, as Spring’s declarative transaction support uses AOP at its foundation. But at a very high level, Spring creates proxies for classes that declare @Transactional on the class itself or on members. … Read more

Spring @Transaction method call by the method within the same class, does not work?

It’s a limitation of Spring AOP (dynamic objects and cglib). If you configure Spring to use AspectJ to handle the transactions, your code will work. The simple and probably best alternative is to refactor your code. For example one class that handles users and one that process each user. Then default transaction handling with Spring … Read more