[英]AspectJ + Spring Boot not weaving included classes within aop.xml
我在使用 LoadTimeWeaving 策略将 AspectJ 与 SpringBoot 集成时遇到了很多问题(由于 Spring AOP 不适合我的情况)。
我配置了很多指南 AspectJ。 这是我在 pom AspectJ 上添加的依赖项
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${ascpectj.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${ascpectj.version}</version>
</dependency>
这是我的 aop.xml
<aspectj>
<weaver options="-showWeaveInfo -debug -Xlintfile:pathToAResource -verbose">
<include within="foo.*"/>
</weaver>
<aspects>
<aspect name="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect"/>
<aspect name="org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect"/>
<aspect name="org.springframework.transaction.aspectj.AnnotationTransactionAspect"/>
<aspect name="org.springframework.cache.aspectj.AnnotationCacheAspect"/>
<aspect name="it.poste.energy.aspects.TaskAspect"/>
</aspects>
</aspectj>
而且我确信它在项目结构中的位置是正确的,因为它实际上是在读取 weaver 标签中传递的选项(我知道 xlintoption 显然不正确,但我认为这对于现在)java
我在基本配置类上配置了自定义 LoadTimeWeaver,如下所示:
@Configuration
@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
public class CustomLoadTimeWeaverConfig implements LoadTimeWeavingConfigurer {
@Override
@NonNull
public LoadTimeWeaver getLoadTimeWeaver() {
return new TomcatLoadTimeWeaver();
}
@Bean
public InstrumentationLoadTimeWeaver loadTimeWeaver() throws Throwable {
return new InstrumentationLoadTimeWeaver();
}
}
然后我启动 spring 应用程序,传递这 2 个 vm 参数作为 java 代理 -javaagent:C:\\Users\\foo-user.m2\\repository\\org\\aspectj\\aspectjweaver\\1.9.6\\aspectjweaver-1.9.6.jar -javaagent :C:\\Users\\foo-user.m2\\repository\\org\\springframework\\spring-instrument\\5.2.10.RELEASE\\spring-instrument-5.2.10.RELEASE.jar
(我尝试按照某处的建议和 EnableLoadTimeWeaving 注释删除 spring-instruments,但没有任何改变)
该应用程序启动良好,但它并没有在 aop.xml 中编织包含包的单个类。 这是织布机调试日志的示例
[URLClassLoader@1efee8e7] debug not weaving 'foo.energy.domain.client.crmu.Foo1'
[URLClassLoader@1efee8e7] debug not weaving 'foo.energy.domain.client.crmu.Foo2'
[URLClassLoader@1efee8e7] debug not weaving 'foo.energy.domain.client.Foo3'
[URLClassLoader@1efee8e7] debug not weaving 'fooo.energy.domain.client.Foo4'
困扰我的一件事是它正确注册了所有方面,如下所示:
[InspectionClassLoader@3db65c0d] info AspectJ Weaver Version 1.9.6 built on Tuesday Jul 21, 2020 at 13:30:08 PDT
[InspectionClassLoader@3db65c0d] info register classloader org.springframework.data.jpa.repository.config.InspectionClassLoader@3db65c0d
[InspectionClassLoader@3db65c0d] info using configuration /C:/Work/Workspaces/foo-project/target/classes/META-INF/aop.xml
[InspectionClassLoader@3db65c0d] info using configuration file:/C:/Users/foo-user/.m2/repository/org/springframework/spring-aspects/5.2.10.RELEASE/spring-aspects-5.2.10.RELEASE.jar!/META-INF/aop.xml
[InspectionClassLoader@3db65c0d] warning Cannot access resource for -Xlintfile:pathToAResource
[InspectionClassLoader@3db65c0d] info register aspect org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect
[InspectionClassLoader@3db65c0d] info register aspect org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect
[InspectionClassLoader@3db65c0d] info register aspect org.springframework.transaction.aspectj.AnnotationTransactionAspect
[InspectionClassLoader@3db65c0d] info register aspect org.springframework.cache.aspectj.AnnotationCacheAspect
[InspectionClassLoader@3db65c0d] info register aspect foo.energy.aspects.TaskAspect
[InspectionClassLoader@3db65c0d] info register aspect org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect
[InspectionClassLoader@3db65c0d] info register aspect org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspect
[InspectionClassLoader@3db65c0d] info register aspect org.springframework.transaction.aspectj.AnnotationTransactionAspect
[InspectionClassLoader@3db65c0d] info register aspect org.springframework.transaction.aspectj.JtaAnnotationTransactionAspect
[InspectionClassLoader@3db65c0d] info register aspect org.springframework.cache.aspectj.AnnotationCacheAspect
[InspectionClassLoader@3db65c0d] info register aspect org.springframework.cache.aspectj.JCacheCacheAspect
[InspectionClassLoader@3db65c0d] 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
[InspectionClassLoader@3db65c0d] info deactivating aspect 'org.springframework.cache.aspectj.JCacheCacheAspect' as it requires type 'javax.cache.annotation.CacheResult' which cannot be found on the classpath
问题是这最后一行日志(方面的注册)被调用了 3/4 次,我不知道这是异常还是什么
我在这里错过了什么吗?
SN:我为 aspectjrt 和 aspectjweaver 使用 Spring Boot v2.3.5.RELEASE、Spring v5.2.10.RELEASE 和 tomcat 9.0.39 和 1.9.6
请注意foo.*
只匹配 foo 包中的类,而不是子包中的类。 为此,您需要foo..*
。 因此,你应该更换
<include within="foo.*"/>
经过
<include within="foo..*"/>
我想知道为什么这么多指南使用
com.*
来编织子包。
我称这种现象为“复制和粘贴编程”。 一些懒惰的人可能想吸引其他人访问他们各自的博客,但他们不做自己的研究或编程,只是复制现有的东西。 它可能都是从某个地方的一个简单的错字开始的,但后来又被复制了一遍又一遍。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.