How to enable with java-based annotations

Did you create an aspect bean in the same @Configuration class? Here’s what the docs suggest: @Configuration @EnableAspectJAutoProxy public class AppConfig { @Bean public FooService fooService() { return new FooService(); } @Bean // the Aspect itself must also be a Bean public MyAspect myAspect() { return new MyAspect(); } }

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

Spring AOP not working for method call inside another method

The aspect is applied to a proxy surrounding the bean. Note that everytime you get a reference to a bean, it’s not actually the class referenced in your config, but a synthetic class implementing the relevant interfaces, delegating to the actual class and adding functionality, such as your AOP. In your above example you’re calling … Read more

Spring AOP: What’s the difference between JoinPoint and PointCut?

Joinpoint: A joinpoint is a candidate point in the Program Execution of the application where an aspect can be plugged in. This point could be a method being called, an exception being thrown, or even a field being modified. These are the points where your aspect’s code can be inserted into the normal flow of … Read more

How to get a method’s annotation value from a ProceedingJoinPoint?

You can get the Signature from a ProceedingJoinPoint and in case of a method invocation just cast it to a MethodSignature. @Around(“execution(public * *(..)) && @annotation(com.mycompany.MyAnnotation)”) public Object procede(ProceedingJoinPoint call) throws Throwable { MethodSignature signature = (MethodSignature) call.getSignature(); Method method = signature.getMethod(); MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class); } But you should first add an annotation attribute. … 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 @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