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

Using Instrumentation to record unhandled exception

I think you should use ASM to manipulate bytecode directly. Here is algoritms: Visit all try/catch blocks (see visitTryCatchBlock) and save all handler labels Visit instructions until one of the handler labels met. After handler label insert logging code GETSTATIC java/lang/System out LDC “exception X occured” INVOKEVIRTUAL java/io/PrintStream println (java/lang/String)V And ensure that your javaagent … 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

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: 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

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