Spring 应用程序重启失败

Spring Application fails to restart

我正在添加端点以重新启动我的启动应用程序。

我遵循了以下 url 的第二个意见(通过创建新上下文重新启动):https://www.baeldung.com/java-restart-spring-boot-app

@RestController
 public class RestartController {
 @RequestMapping(value = "/restart-admin", method = RequestMethod.POST)
    public void restart() {    
            ActivitiAdminCrmMain.restart();
    }

}
public class ActivitiAdminCrmMain {
private static ConfigurableApplicationContext context;

public static void main(String[] args) {
    context = SpringApplication.run(ActivitiAdminCrmMain.class, args);
}

public static void restart() {
    ApplicationArguments args = context.getBean(ApplicationArguments.class);

    Thread thread = new Thread(() -> {
        context.close();
        context = SpringApplication.run(ActivitiAdminCrmMain.class, args.getSourceArgs());
    });

    thread.setDaemon(false);
    thread.start();
}

我遇到以下错误:

Exception in thread "Thread-4" java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [META-INF/spring.factories]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
    at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1325)
    at org.apache.catalina.loader.WebappClassLoaderBase.findResources(WebappClassLoaderBase.java:948)
    at java.lang.ClassLoader.getResources(ClassLoader.java:1142)
    at org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(SpringFactoriesLoader.java:110)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:393)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:385)
    at org.springframework.boot.SpringApplication.initialize(SpringApplication.java:261)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:237)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
    at storebrand.activiti.admin.ActivitiAdminCrmMain.lambda[=12=](ActivitiAdminCrmMain.java:35)
    at java.lang.Thread.run(Thread.java:748)
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
</dependency>

有了上面的 devtools 依赖,你可以试试下面的方法。

@RestController
 public class RestartController {
 @RequestMapping(value = "/restart-admin", method = RequestMethod.POST)
    public void restart() {    
        org.springframework.boot.devtools.restart.Restarter.getInstance().restart();
    }

}