尝试声明 InstrumentationLoadTimeWeaver bean,而不是显式使用 -javaagent:/path/to/org.springframework.instrument-{version}.jar。根据文档
要使用它,您必须通过提供以下 JVM 选项来使用 Spring 代理启动虚拟机:
-javaagent:/path/to/org.springframework.instrument-{version}.jar
请注意,这需要修改 VM 启动脚本,这可能会阻止您在应用程序服务器环境中使用它(取决于您的操作策略)。此外,JDK 代理将检测整个 VM,这可能会很昂贵。
我希望按照下面的方式做会更好。
@Bean
public InstrumentationLoadTimeWeaver loadTimeWeaver() throws Throwable {
InstrumentationLoadTimeWeaver loadTimeWeaver = new InstrumentationLoadTimeWeaver();
return loadTimeWeaver;
}
同样可以在xml配置中完成。
找到了一个新库,它可以解决动态设置 spring InstrumentationLoadTimeWeaver 以启用对方面的支持,而无需使用显式 java 代理启动 JVM
<dependency>
<groupId>de.invesdwin</groupId>
<artifactId>invesdwin-instrument</artifactId>
<version>1.0.2</version>
</dependency>
春季启动配置
@SpringBootApplication
/**
* Make @Configurable work via @EnableLoadTimeWeaving.
* If it does not work, alternatively you can try:
* @ImportResource(locations = "classpath:/META-INF/ctx.spring.weaving.xml")
*/
@EnableLoadTimeWeaving
public class MySpringBootApplication {
public static void main(final String[] args) {
DynamicInstrumentationLoader.waitForInitialized(); //dynamically attach java agent to jvm if not already present
DynamicInstrumentationLoader.initLoadTimeWeavingContext(); //weave all classes before they are loaded as beans
SpringApplication.run(MySpringBootApplication.class, args); //start application, load some classes
}
}