【问题标题】:Spring-Boot 2 AspectJ Load Time WeavingSpring-Boot 2 AspectJ 加载时间编织
【发布时间】:2019-05-10 15:00:27
【问题描述】:

我正在试验 Spring Boot 2 和 AspectJ Load Time Weaving。 我能够从 Eclipse 运行测试和应用程序(向 VM 添加两个代理:aspectjweaver 和 spring-instrument),来自 Maven 的surefire 也运行良好,但是我无法向 spring-boot-maven-plugin 添加两个代理。

这是我的 pom.xml sn-p

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>
            -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
            -javaagent:"${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar"
            -Dspring.profiles.active=test</argLine>
    </configuration>
</plugin>
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <agent>
            ${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
        </agent>
        <agent>
            ${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar
       </agent>
    </configuration>
</plugin>

看起来 spring-boot-maven-plugin 仅将 spring-instrument 附加到 VM。 (实际上,总是 pom 的最后一个“代理”条目。

任何人有一个想法,如何让它工作?

我正在使用 Java 8、Spring Boot 2.1.0.RELEASE(AspectJ 1.9.2 和 Spring 5.1.2.RELEASE)。

【问题讨论】:

    标签: spring-boot aspectj spring-boot-maven-plugin load-time-weaving


    【解决方案1】:

    根据plugin documentationagent 参数是一个文件数组,即应该有一种方法可以指定多个文件。查看源代码,我还可以看到,如果实际上指定了多个代理,它们都将添加到命令行参数列表中。

    现在悬而未决的问题是如何指定多个代理?通常在 Maven 插件中,如果有一个数组元素,则命名是带有复数“s”的东西,例如“agents”,在里面你会使用单数的“agent”作为元素。但是这里的数组元素已经是单数了。

    为什么 Spring 团队不在这里这样做,我不知道。但是当试验 Maven 参数-X 并检查调试输出时,我发现无论你使用什么子标签,它都可以工作。您可以使用任何一种

    <agent>
        <agent>foo</agent>
        <agent>bar</agent>
    </agent>
    
    <agent>
        <x>foo</x>
        <y>bar</y>
    </agent>
    
    <agent>
        <agentElement>foo</agentElement>
        <agentElement>bar</agentElement>
    </agent>
    

    由于缺乏更好的约定,我推荐后者。无论如何,仅仅重复两个顶级agent标签并不能解决问题。

    没有完整项目的我没有测试的是命令行上的两个代理是否真的正确启动了Spring Boot。这部分由您决定。

    更新:我刚刚提出了相应的Spring Boot issue #15455

    更新 2: Spring Boot 问题已解决,在 2.2.0 版本中参数应重命名为 agents,因此在内部使用多个 agent 条目会更直观它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多