如何解决AspectJ + Spring Boot 未在 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 中编织包含包的单个类。这是weaver的调试日志示例
[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.*
来编织子包。
我称这种现象为“复制和粘贴编程”。一些懒惰的人可能想吸引其他人访问他们各自的博客,但不会自己进行研究或编程,而只是复制现有内容。这一切可能都是从某个地方的一个简单的错字开始的,但后来又被复制了一遍又一遍。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。