I am trying to set up AspectJ load-time weaving (LTW) for logging method execution time in my Java web application. It is a webapp written using Servlets, jsp (No Spring used) and deployed on jBoss EAP 7.2 written using Servlets and jsp (No Spring used).
I have created an aspect and packaged it into a JAR file along with the aop.xml configuration file. However, when I run my application, I encounter the following error: The specified aspect ‘AspectLogger’ cannot be found.
Here is the full log in jBoss console:
2024-08-05 09:45:05,159 ERROR [stderr] (MSC service thread 1-2) [ModuleClassLoader@17497425] info AspectJ Weaver Version 1.9.2 built on Wednesday Oct 24, 2018 at 15:43:33 GMT
2024-08-05 09:45:05,160 ERROR [stderr] (MSC service thread 1-2) [ModuleClassLoader@17497425] info register classloader org.jboss.modules.ModuleClassLoader@17497425
2024-08-05 09:45:05,161 ERROR [stderr] (MSC service thread 1-2) [ModuleClassLoader@17497425] info using configuration /C:/apps/jboss-eap-7.2.0/standalone/lib/aop.xml
2024-08-05 09:45:05,163 ERROR [stderr] (MSC service thread 1-2) [ModuleClassLoader@17497425] info register aspect AspectLogger
2024-08-05 09:45:05,164 ERROR [stderr] (MSC service thread 1-2) [ModuleClassLoader@17497425] error The specified aspect 'AspectLogger' cannot be found
Here is what I have done so far.
1. Project Structure:
AspectLogger.java
package com.sample;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
public class AspectLogger {
@Around("execution(* com.webservices..*(..))")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
Object proceed = joinPoint.proceed();
long executionTime = System.currentTimeMillis() - start;
System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
return proceed;
}
}
aop.xml
<aspectj>
<weaver options="-verbose -showWeaveInfo">
<include within="com.webservices..*"/>
</weaver>
<aspects>
<aspect name="AspectLogger"/>
</aspects>
</aspectj>
To package the aspect as jar file I export the project as jar in Eclipse which created the jar with below structure.(Do I have to use ajc command to compile and package? If so, please provide me the suitable command. Tried different arguments with ajc to complie AspectLogger.java into AspectLogger.class, but still got the same error.)
C:.
| .classpath
| .project
|
+---com
| ---sample
| AspectLogger.class
|
+---lib
| aspectj-1.9.2.jar
| aspectjrt-1.9.2.jar
| aspectjtools-1.9.2.jar
| aspectjweaver-1.9.2.jar
|
---META-INF
aop.xml
MANIFEST.MF
Java Agent Argument:
I am using the following JVM argument to enable AspectJ weaving:
set "ASPECT_HOME=C:appsjboss-eap-7.2.0standalonelib"
set "JAVA_OPTS=%JAVA_OPTS% -Djboss.modules.system.pkgs=org.aspectj -Dorg.aspectj.weaver.loadtime.debug=true -Dorg.aspectj.weaver.loadtime.configuration=%ASPECT_HOME%aop.xml -Xbootclasspath/a:%ASPECT_HOME%aspectjweaver-1.9.2.jar:%ASPECT_HOME%SampleLogger.jar -Dorg.aspectj.tracing.enabled=false -Dorg.aspectj.tracing.factory=default"
rem Uncomment below line to enable Aspect load-time weaving
set "JAVA_OPTS=%JAVA_OPTS% -javaagent:%ASPECT_HOME%aspectjweaver-1.9.2.jar -cp %ASPECT_HOME%*"
Here is the content of my %ASPECT_HOME%
When I start the jboss, the server is starting and all my webapps are deployed successfully. Even though I specified aop.xml in -Dorg.aspectj.weaver.loadtime.configuration=%ASPECT_HOME%aop.xml , I still kept it in META-INF just to be safe. If removed it in JVM options and only have the aop.xml in META-INF, it is not even found by weaver and nothing is coming up in jboss logs.
Please help me identify what the issue is and help getting the logger running.