gpt4 book ai didi

spring-boot - Tomcat 8、Spring Boot、@Configurable LoadTimeWeaving 没有 -javaagent?

转载 作者:行者123 更新时间:2023-12-03 23:58:59 32 4
gpt4 key购买 nike

我正在尝试设置一个 @Configurable域对象(不由 spring 容器管理)。
我通过添加 -javaagent:path/to/spring-instrument.jar 来解决这个问题作为一个 JVM 参数,但我不是 100% 清楚这个 -javaagent 是否必须到位。我在 Tomcat 8 上运行它。我可能误解了 documentation但似乎我可以使用另一种机制来实现这一点,特别是这一行:

Do not define TomcatInstrumentableClassLoader anymore on Tomcat 8.0 and higher. Instead, let Spring automatically use Tomcat’s new native InstrumentableClassLoader facility through the TomcatLoadTimeWeaver strategy.



下面的代码示例:
@SpringBootApplication
@EnableLoadTimeWeaving
public class TestApplication {

public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
@Bean
public MyService myService(){
return new MyService();
}
}
@Configurable
public class MyDomainObject {

@Autowired
private MyService myService;

public MyService getMyService(){
return myService;
}
}
 public class MyService {
private static final Logger log = LoggerFactory.getLogger(MyService.class);

public void test(){
log.info("test");
}
}

那么有没有办法在不指定 -javaagent 的情况下编织这些 @Configrable 对象?我有兴趣了解在作为 WAR 部署到独立 Tomcat 8 服务器和/或在作为“胖”jar 启动时使用嵌入式 Tomcat 8 服务器时是否可以完成此操作。

目前部署到 Standalone Tomcat 8 服务器不会引发错误,但会引发 getMyService()上面的方法返回null。作为胖 jar 启动会在启动期间引发以下错误:
Caused by: java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar
我想真正的问题是我如何 Specify a custom LoadTimeWeaver在 Tomcat 8 中?正如文档所述,似乎没有什么会自动发生,但我可能再次误解了这到底意味着什么。

最佳答案

你可以试试这个:

@Bean
public InstrumentationLoadTimeWeaver loadTimeWeaver() {
return new InstrumentationLoadTimeWeaver();
}

或者

有一个新库可以解决动态设置 spring InstrumentationLoadTimeWeaver 以启用对方面的支持,而无需使用显式 java 代理启动 JVM
<dependency>
<groupId>de.invesdwin</groupId>
<artifactId>invesdwin-instrument</artifactId>
<version>1.0.2</version>
</dependency>

@SpringBootApplication
@EnableLoadTimeWeaving
public class TestApplication{
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(TestApplication.class, args); //start application, load some classes
}
}

关于spring-boot - Tomcat 8、Spring Boot、@Configurable LoadTimeWeaving 没有 -javaagent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33158039/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com