我有一个复杂的 java spring boot 项目,其中一个类 MyClass
有一个静态属性,需要 30 秒才能从数据库加载。 MyClass
被注解为@SpringBootApplication
和方法
@Bean
public CommandLineRunner demo() {...
在应用程序启动时执行。
在开发过程中,当我调试/纠正项目的其他部分需要 MyClass
的静态属性时,我想避免这 30 秒。
我想利用 Intellij IDEA 中的 compiler.automake.allow.when.app.running
注册表设置,以便通过热插拔再次加载更改的类。
我怎样才能避免 MyClass
每次我在其他地方更改一个类时一次又一次地执行(分别重新启动整个项目)?
最佳答案
您可以使用 @RefreshScope
创建一个单独的类,其中静态变量加载值并且不要保持静态。
@Componen
@RefreshScope
public class MyClass {
private String myRefreshableObject;
@PostConstruct
public void init() {
// set the values of myRefreshableObject
}
public void getMyRefreshableObject() {
return this.myRefreshableObject;
}
}
现在,每当您想重新加载 bean 时,您都会抛出一个 POST
请求执行器端点 refresh
,如下所示。
curl -d{} http://localhost:8080/refresh
这里需要以下Spring Boot依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
关于java - Intellij IDEA 和 Spring Boot : how to restart only parts of my java code?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42525676/