I'm creating simple experimental program, which use LTW to weave aspect into a class, however aspect load-time weaving doesn't happening. You can check and use code, which I will describe later, in GitHub. I'm using Java 17 and IntelliJ IDE.
Used aspect:
@Aspect
@Component
public class MonitoringAspect {
@Pointcut("execution(* com.sj.utilities.Watch.*(..))")
public void monitoringOperation(){}
@Around("monitoringOperation()")
public Object aroundAdvice(ProceedingJoinPoint pjp)
throws Throwable {
System.out.println("Before " + pjp.getSignature() + " execution!");
long startTime = System.nanoTime();
Object retVal = pjp.proceed();
long endTime = System.nanoTime();
long executingTime = endTime - startTime;
System.out.println("Method's execution time:\n"
+ "\t" + executingTime + " ns\n"
+ "\t" + (executingTime / 1000) + " μs\n"
+ "\t" + (executingTime / 1000000) + " ms\n"
+ "\t" + (executingTime / 1000000000) + " s");
System.out.println("After " + pjp.getSignature() + " execution!");
return retVal;
}
}
I tried to remove @Component, because aspect bean is not required, however it changed nothing.
Advised by aspect is simple method sleeping of Watch class, with implementation of FictiveWatch interface (Interface exists only for Spring to use JDK dynamic proxies).
Created Java annotation config:
@Configuration
//@EnableAspectJAutoProxy
@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
@ComponentScan("com.sj")
public class AOPConfig implements LoadTimeWeavingConfigurer {
@Override
public LoadTimeWeaver getLoadTimeWeaver() {
return new InstrumentationLoadTimeWeaver();
}
@Bean
public InstrumentationLoadTimeWeaver loadTimeWeaver() throws Throwable {
return new InstrumentationLoadTimeWeaver();
}
}
As well as xml config:
<?xml version="1.0" encoding="UTF-8"?>
<beans <!-- namespaces ...-->
<!-- this switches on the load-time weaving -->
<context:load-time-weaver
weaver-class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
<!-- this switches on the spring aop autoproxy -->
<!--<aop:aspectj-autoproxy/>-->
<!-- a service object; we will be profiling its methods -->
<bean id="watch"
class="com.sj.utilities.Watch"/>
<bean id="monitoringAspect"
class="com.sj.aspects.MonitoringAspect"/>
</beans>
To use weave aspect I use this META-INF/aop.xml file:
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "https://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<weaver
options="-verbose -showWeaveInfo">
<!-- only weave classes in our application-specific packages -->
<include within="com.sj..*"/>
</weaver>
<aspects>
<!-- weave in just this aspect -->
<aspect name="com.sj.aspects.MonitoringAspect"/>
</aspects>
</aspectj>
Driver class:
public class Main {
public static void main(String[] args) throws InterruptedException {
//Check used JVM arguments
System.out.println(ManagementFactory.getRuntimeMXBean().getInputArguments());
//Java class LTW config
//ApplicationContext context = new AnnotationConfigApplicationContext(AOPConfig.class);
//XML LTW config
ApplicationContext context = new ClassPathXmlApplicationContext("config/aop-xml-conf.xml");
Watch watch = context.getBean(Watch.class);
watch.sleeping(3000);
}
}
In the driver class, I use AnnotationConfig or XMLConfig to use LTW, but weaving isn't happening, so crosscutting functionality isn't applied. As well as I print used by JVM arguments.
The only thing I see, when using AnnotationConfig or XMLConfig, is JVM arguments that I use:
[--add-opens=java.base/java.lang=ALL-UNNAMED, -Dspring.aop.proxy-target-class=false, -Dorg.aspectj.weaver.loadtime.configuration=trace, -Dorg.aspectj.weaver.loadtime.verbose=true, -javaagent:E:\intelliJIdea_dir\IntelliJ IDEA Community Edition 2022.2.1\lib\idea_rt.jar=55606:E:\intelliJIdea_dir\IntelliJ IDEA Community Edition 2022.2.1\bin, -Dfile.encoding=UTF-8]
As well as result of Watch class sleeping method:
Slept 3000 ms
Why weaving is not happening, which part of written code is not right? As per Spring documentation on LTW, as well as other answers on questions for LTW topic everything should be alright, or I'm missing something?