简体   繁体   中英

AspectJ LTW (weaving) not working with Spring Boot

I'm on Spring Boot 2.1.2.RELEASE - Java 11 - Fat JAR

Following the documentation, I have:

added the required dependencies to the Gradle build

implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.aspectj:aspectjrt:1.9.2'
implementation 'org.aspectj:aspectjweaver:1.9.2'

enabled LoadTimeWeaving

@SpringBootApplication
@EnableLoadTimeWeaving
public class MyApplication { ... }

provided the aop.xml under META-INF

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver>
        <!-- only weave classes in our application-specific packages -->
        <include within="my.base.package.*" />
    </weaver>

    <aspects>
        <!-- weave in just this aspect -->
        <aspect name="my.base.package.spring.aop.MyAspects" />
    </aspects>
</aspectj>

created the new @Aspect class

@Aspect
public class MyAspects {
    @Around("methodsToBeProfiled()")
    public Object profile(final ProceedingJoinPoint pjp) throws Throwable {
        final var sw = new StopWatch(getClass().getSimpleName());
        try {
            sw.start(pjp.getSignature().getName());
            return pjp.proceed();
        } finally {
            sw.stop();
            System.out.println(sw.prettyPrint());
        }
    }

    @Pointcut("execution(* my.base.package.other.MyClass.*(..))")
    public void methodsToBeProfiled() {}
}

added the Jar for instrumentation

-javaagent:/home/myuser/spring-instrument-5.1.5.RELEASE.jar

Log, which as you see, show MyAspects as recognized

[AppClassLoader@2c13da15] info AspectJ Weaver Version 1.9.2 built on Wednesday Oct 24, 2018 at 15:43:33 GMT
[AppClassLoader@2c13da15] info register classloader jdk.internal.loader.ClassLoaders$AppClassLoader@2c13da15
[AppClassLoader@2c13da15] info using configuration /home/edoardo/IdeaProjects/scheduler/scheduler-engine/out/production/resources/META-INF/aop.xml
[AppClassLoader@2c13da15] info using configuration file:/home/edoardo/.gradle/caches/modules-2/files-2.1/org.springframework/spring-aspects/5.1.5.RELEASE/3bb95e05b646ef93e2a4cf0b600924c2979fc723/spring-aspects-5.1.5.RELEASE.jar!/META-INF/aop.xml
[AppClassLoader@2c13da15] info register aspect my.base.package.spring.aop.MyAspects
[AppClassLoader@2c13da15] info register aspect org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect
[AppClassLoader@2c13da15] info register aspect org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect
[AppClassLoader@2c13da15] info register aspect org.springframework.transaction.aspectj.AnnotationTransactionAspect
[AppClassLoader@2c13da15] info register aspect org.springframework.transaction.aspectj.JtaAnnotationTransactionAspect
[AppClassLoader@2c13da15] info deactivating aspect 'org.springframework.transaction.aspectj.JtaAnnotationTransactionAspect' as it requires type 'javax.transaction.Transactional' which cannot be found on the classpath
[AppClassLoader@2c13da15] info register aspect org.springframework.cache.aspectj.AnnotationCacheAspect
[AppClassLoader@2c13da15] info register aspect org.springframework.cache.aspectj.JCacheCacheAspect
[AppClassLoader@2c13da15] info deactivating aspect 'org.springframework.cache.aspectj.JCacheCacheAspect' as it requires type 'org.springframework.cache.jcache.interceptor.JCacheAspectSupport' which cannot be found on the classpath
[AppClassLoader@2c13da15] info deactivating aspect 'org.springframework.cache.aspectj.JCacheCacheAspect' as it requires type 'javax.cache.annotation.CacheResult' which cannot be found on the classpath
[AppClassLoader@2c13da15] warning javax.* types are not being woven because the weaver option '-Xset:weaveJavaxPackages=true' has not been specified

However MyAspects is never instantiated (so no debug), and methods are not being weaved with my aspect code.

Did I miss something?


Edit: seems both Jars, aspectjweaver and spring-instrument are required as agents. Is this normal?

You do need the aspectjweaver agent jar. You don't need spring-instrument (ever for a fat jar - AFAIK it was used in app servers to work around some historical issues with their class loaders). You also don't need to @EnableLoadTimeWeaving (also an app server feature, redundant if you control the agent). Also (minor niggle) aspectjrt is a transitive dependency of aspectjweaver , so you don't need both. So you have it working it seems, even though you have done more work than you needed. Sample Spring Boot apps with various weaving options: here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM