AspectJ LTW(编织)不适用于Spring Boot

时间:2019-02-18 14:09:19

标签: spring spring-boot aspectj

我在Spring Boot 2.1.2.RELEASE-Java 11-胖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 { ... }

aop.xml下提供了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>

创建了一个新的@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() {}
}

添加了用于检测的罐子

-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作为代理。这正常吗?

1 个答案:

答案 0 :(得分:1)

您确实需要aspectjweaver代理jar。您不需要spring-instrument(对于一个胖子罐来说,它都是AFAIK-在应用程序服务器中用于解决其类加载器的某些历史问题)。您也不需要@EnableLoadTimeWeaving(这也是应用服务器功能,如果您控制代理,则是多余的)。另外,(小指)aspectjrtaspectjweaver的传递依赖,因此您不需要两者。因此,即使您完成了比所需的工作还要多的工作,您似乎还是可以正常工作。具有各种编织选项的示例Spring Boot应用程序:here