I am using aspect oriented programming for separating logging concern in my project but while deployment I am getting following error in tomcat while deploying the application
error Cannot register 'com.temp.request.util.aspect.TraceLogger' because the type found with that name is not an aspect
TraceLogger class is as follows
package com.temp.request.util.aspect;
import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TraceLogger {
public Object logMethodEntryAndExit(ProceedingJoinPoint joinPoint) throws Throwable {
LOG.trace(joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "() begins with " + Arrays.toString(joinPoint.getArgs()));
Object result = joinPoint.proceed();
LOG.trace(joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "() ends with " + result);
return result;
}
}
I have declared aspect and TraceLogger bean in spring.xml file as follows
<bean id="TraceLogger" class="com.temp.request.util.aspect.TraceLogger" />
<aop:config>
<aop:aspect id="methodLogger" ref="TraceLogger">
<aop:pointcut id="logMethod"
expression="execution(* com.temp.request..*.*(..)) and !execution(* com.temp.request.listener.*.*(..)) and !execution(* com.temp.request.ws.*.*(..)) and !execution(* .util.*.*(..))" />
<aop:around pointcut-ref="logMethod" method="logMethodEntryAndExit" />
</aop:aspect>
</aop:config>
dependency declared in dependencies.gradle as follows
compile (
'org.aspectj:aspectjrt:1.8.4',
'org.aspectj:aspectjweaver:1.8.4',
'org.springframework:spring-instrument-tomcat:4.0.9.RELEASE'
)
Using load time weaving and specified following line in spring.xml as follows
<context:load-time-weaver aspectj-weaving="on" weaver-class="org.springframework.instrument.classloading.tomcat.TomcatLoadTimeWeaver" />
Loading spring.xml by specifying contextConfigLocation in web.xml as follows
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring.xml</param-value>
</context-param>
Loading the aspect by specifying aspect name in aop.xml located in META-INF folder of my project as follows
<aspectj>
<weaver options="-Xset:weaveJavaxPackages=true">
<include within="javax.jms.*"/>
</weaver>
<aspects>
<aspect name="com.temp.request.util.aspect.TraceLogger" />
</aspects>
</aspectj>
Why tomcat is not able to find TraceLogger aspect while doing load time weaving ?
I think you are having a conceptual problem here:
<aop:config>
.Please note that AspectJ is not the same as Spring AOP, you should decide which of the two to use. I suggest to stick with Spring AOP for now and ditch the whole LTW stuff.
Looking at your Spring AOP aspect declaration <aop:aspect id="methodLogger" ref="TraceLogger">
and the error message you get, I would think that you forgot to declare a Spring bean named "TraceLogger". If an aspect is not a Spring bean/component, you cannot use it as an aspect.
I warmly recommend to read the Spring AOP manual.
If you ever reach the limits of Spring AOP and want to explore the more powerful AspectJ there also is a chapter about how to configure AspectJ via LTW. Please note that AspectJ is completely independent of Spring, you can use it inside of Spring projects or for any POJO or non-Spring container application you like, even for JVM languages other than Java.