简体   繁体   English

AspectJ + Spring Boot 未在 aop.xml 中编织包含的类

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

I'm having quite a few problems integrating AspectJ with SpringBoot using the LoadTimeWeaving strategy (due Spring AOP doesn't fit my case).我在使用 LoadTimeWeaving 策略将 AspectJ 与 SpringBoot 集成时遇到了很多问题(由于 Spring AOP 不适合我的情况)。

I configured following many guides AspectJ.我配置了很多指南 AspectJ。 Here are the dependencies I added on my pom AspectJ related这是我在 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>

Here is my aop.xml这是我的 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>

And I'm sure it's correctly positioned within the project structure due to the fact that it's actually reading the options in pass in the weaver tag (I know that the xlintoption isn't obviously correct, but I don't think it really matters for now)java而且我确信它在项目结构中的位置是正确的,因为它实际上是在读取 weaver 标签中传递的选项(我知道 xlintoption 显然不正确,但我认为这对于现在)java

I configured the custom LoadTimeWeaver on a basic config class as so:我在基本配置类上配置了自定义 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();
    }

}

I then launch the spring app passing these 2 vm arguments as for java agents -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然后我启动 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

(I tried removing spring-instruments as somewhere suggested and EnableLoadTimeWeaving annotation, but nothing changed) (我尝试按照某处的建议和 EnableLoadTimeWeaving 注释删除 spring-instruments,但没有任何改变)

The app starts fine but it does not weave a single class of the included packages within aop.xml.该应用程序启动良好,但它并没有在 aop.xml 中编织包含包的单个类。 This is an example of the debug logs of the 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'

One thing that bugs me is the fact it registers all aspects correctly as so:困扰我的一件事是它正确注册了所有方面,如下所示:

[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

Problem is that these last line of logs (the registering of the aspects) get called like 3/4 times and I don't know whether it's abnormal or what问题是这最后一行日志(方面的注册)被调用了 3/4 次,我不知道这是异常还是什么

Am I missing something here?我在这里错过了什么吗?

SN: I'm using Spring Boot v2.3.5.RELEASE, Spring v5.2.10.RELEASE and tomcat 9.0.39 and 1.9.6 for both aspectjrt and aspectjweaver SN:我为 aspectjrt 和 aspectjweaver 使用 Spring Boot v2.3.5.RELEASE、Spring v5.2.10.RELEASE 和 tomcat 9.0.39 和 1.9.6

Please note that foo.* only matches classes directly in the foo package, not those in subpackages.请注意foo.*只匹配 foo 包中的类,而不是子包中的类。 For that you need foo..* .为此,您需要foo..* Therefore, you ought to replace因此,你应该更换

<include within="foo.*"/>

by经过

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

I wonder why so many guides use com.* to weave also subpackages.我想知道为什么这么多指南使用com.*来编织子包。

I call this phenomenon "copy & paste programming".我称这种现象为“复制和粘贴编程”。 Some lazy people might want to attract other people to their respective blogs, but not do their own research or programming, simply reproducing existing stuff.一些懒惰的人可能想吸引其他人访问他们各自的博客,但他们不做自己的研究或编程,只是复制现有的东西。 It might all have started with a simple typo somewhere, but then it was copied and copied again.它可能都是从某个地方的一个简单的错字开始的,但后来又被复制了一遍又一遍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM