AspectJ LTW(编织)不适用于Spring Boot [英] AspectJ LTW (weaving) not working with Spring Boot

查看:567
本文介绍了AspectJ LTW(编织)不适用于Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

根据文档,我有:

将所需的依赖项添加到了Gradle构建中

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'

已启用LoadTimeWeaving

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

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为已识别

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


但是MyAspects从未实例化(因此没有调试),并且没有使用我的方面代码来编织方法.


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

我错过了什么吗?

似乎Jars,aspectjweaverspring-instrument都需要作为代理.这正常吗?

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

推荐答案

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

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.

这篇关于AspectJ LTW(编织)不适用于Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆