簡體   English   中英

AspectJ LTW(編織)不適用於 Spring Boot

[英]AspectJ LTW (weaving) not working with Spring Boot

我在Spring Boot 2.1.2.RELEASE - Java 11 - Fat JAR

根據文檔,我有:

將所需的依賴項添加到 Gradle 構建中

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

啟用LoadTimeWeaving

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

提供了META-INF下的aop.xml

<!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>

創建了新的@Aspect

@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() {}
}

添加了用於儀器的 Jar

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

日志,如您所見,將MyAspects顯示為已識別

[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

然而, MyAspects從來沒有被實例化(所以沒有調試),並且方法沒有被我的方面代碼編織。

我錯過了什么?


編輯:似乎 Jars、 aspectjweaverspring-instrument都需要作為代理。 這是正常的嗎?

您確實需要aspectjweaver代理 jar。 你不需要spring-instrument (對於一個胖罐子 - AFAIK 它被用於應用服務器來解決他們的類加載器的一些歷史問題)。 您也不需要@EnableLoadTimeWeaving (也是一個應用程序服務器功能,如果您控制代理則是多余的)。 此外(小問題) aspectjrtaspectjweaver的傳遞依賴aspectjweaver ,因此您不需要兩者。 因此,即使您完成的工作超出了您的需要,您似乎也可以使用它。 具有各種編織選項的示例 Spring Boot 應用程序:這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM