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

java.lang.IllegalStateException: No thread-bound request found, exception in aspect

You shouldn’t autowire a HttpServletRequest in your aspect as this will tie your aspect to be only runnable for classes that are called from within an executing HttpServletRequest. Instead use the RequestContextHolder to get the request when you need one. private String getRemoteAddress() { RequestAttributes attribs = RequestContextHolder.getRequestAttributes(); if (attribs instanceof NativeWebRequest) { HttpServletRequest request … Read more

Unit testing Spring @Around AOP methods

You can test a Spring Aspect by creating a proxy programatically: MyInterface target = new MyClass(); AspectJProxyFactory factory = new AspectJProxyFactory(target); MyAspect aspect = new MyAspect(arg); factory.addAspect(aspect); MyInterface proxy = factory.getProxy(); … then you can call methods on proxy, and make assertions about aspect, proxy and target.

What is AspectJ good for? [closed]

permission check interrupt action that takes too long run action in separate thread or even in context of different process or event on other machine monitoring preparing any data / environment before call and processing results after call opening / closing resources EDIT Although many years passed since I gave this answer I decided to … Read more

Are there alternatives to cglib? [closed]

ASM java-asm CGLIB and almost all other libraries are built on top of ASM which itself acts on a very low level. This is a show-stopper for most people as you have to understand the byte code and a little bit of the JVMS to use it properly. But mastering ASM is most certainly very … 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

@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