AspectJ + Spring aop.xml 内 类 未编织的引导

AspectJ + Spring Boot not weaving included classes within aop.xml

我在使用 LoadTimeWeaving 策略将 AspectJ 与 SpringBoot 集成时遇到了很多问题(由于 Spring AOP 不适合我的情况)。

我按照许多指南配置了 AspectJ。这是我在我的 pom AspectJ related

上添加的依赖项
    <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

我在基本配置 class 上配置了自定义 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 代理 -java代理:C:\Users\foo-user.m2\repository\org\aspectj\aspectjweaver.9.6\aspectjweaver-1.9.6.jar -java代理:C:\Users\foo-user.m2\repository\org\springframework\spring-instrument.2.10.RELEASE\spring-instrument-5.2.10.RELEASE.jar

(我尝试按照某处的建议删除 spring-instruments 和 EnableLoadTimeWeaving 注释,但没有任何改变)

该应用程序启动正常,但它没有在 aop.xml 中编织一个 class 包含的包。这是 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:我正在使用 Spring Boot v2.3.5.RELEASE、Spring v5.2.10.RELEASE 和 tomcat 9.0.39和 1.9.6 对于 aspectjrt 和 aspectjweaver

请注意,foo.* 只匹配 foo 包中的 类,而不匹配子包中的那些。为此,您需要 foo..*。因此,你应该更换

<include within="foo.*"/>

来自

<include within="foo..*"/>

I wonder why so many guides use com.* to weave also subpackages.

我把这种现象称为“复制粘贴编程”。一些懒惰的人可能想吸引其他人访问他们各自的博客,但自己不做研究或编程,只是简单地复制现有的东西。可能一切都是从某个地方的一个简单的错字开始的,但后来又被复制了又复制了。