AspectJ logs in with Log4j

I am trying to inject AspectJ logging into a module to better analyze it. My application already uses the log4J framework.
If I run the AspectJ class in a separate application, it works fine, but it does not work when I integrate it with the application. Here is what i did
applicationContext.xml

<aop:aspectj-autoproxy/> <bean id="logAspectj" class="com.test.aspect.LoggingAspect"/> 

Class LoggingAspect

 @Component @Aspect public class LoggingAspect { private final Log log = LogFactory.getLog(this.getClass()); @Around("execution(public void com.db.TestMarshaller.saveDoc(..))") public Object logTimeMethod(ProceedingJoinPoint joinPoint) throws Throwable { StopWatch stopWatch = new StopWatch(); stopWatch.start(); Object retVal = joinPoint.proceed(); stopWatch.stop(); StringBuffer logMessage = new StringBuffer(); logMessage.append(joinPoint.getTarget().getClass().getName()); logMessage.append("."); log.info(logMessage.toString()); return retVal; } } 

Note. Here TestMarshaller is not displayed through Spring.

Is there any specific AspectJ setup for Log4j?

0
source share
1 answer

since your com.db.TestMarshaller is not "spring managed" (there is no Spring bean in your context), the Spring namespace used will NOT create a proxy that invokes your aspect.

In this case, you will either have to compile your sources using the AspectJ Compiler (Compile Time Weaving) or use the runtime version of aspectj to change your bytecode when loading your class (Time Time Weaving).

Actually, it depends on your use case, which one you go to - if your Aspect is part of your business logic and cannot change at runtime, use the aspectj compiler as it will be executed only once. In case you need a more dynamic application of your aspect, use load time friction instead.

Hope this helps, Jochen

+1
source

All Articles