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 { ... }
提供 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
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() {}
}
添加了用于检测的 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,aspectjweaver
和 spring-instrument
作为代理。这正常吗?
您确实需要 aspectjweaver
代理 jar。你不需要 spring-instrument
(对于一个胖罐子 - AFAIK 它被用于应用程序服务器来解决他们的 class 加载器的一些历史问题)。您也不需要 @EnableLoadTimeWeaving
(也是应用程序服务器功能,如果您控制代理则多余)。另外(小问题)aspectjrt
是 aspectjweaver
的传递依赖,因此您不需要两者。所以你看起来好像在工作,即使你做了比你需要的更多的工作。示例 Spring 使用各种编织选项启动应用程序:here。
我在 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 { ... }
提供 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
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() {}
}
添加了用于检测的 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,aspectjweaver
和 spring-instrument
作为代理。这正常吗?
您确实需要 aspectjweaver
代理 jar。你不需要 spring-instrument
(对于一个胖罐子 - AFAIK 它被用于应用程序服务器来解决他们的 class 加载器的一些历史问题)。您也不需要 @EnableLoadTimeWeaving
(也是应用程序服务器功能,如果您控制代理则多余)。另外(小问题)aspectjrt
是 aspectjweaver
的传递依赖,因此您不需要两者。所以你看起来好像在工作,即使你做了比你需要的更多的工作。示例 Spring 使用各种编织选项启动应用程序:here。