Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 什么时候依靠弹簧是安全的;动情前期?_Spring_Spring Boot_Spring Annotations - Fatal编程技术网

Spring 什么时候依靠弹簧是安全的;动情前期?

Spring 什么时候依靠弹簧是安全的;动情前期?,spring,spring-boot,spring-annotations,Spring,Spring Boot,Spring Annotations,根据Spring的文档,我添加了一个关机挂钩: SpringApplication app = new SpringApplication(App.class); DefaultProfileUtil.addDefaultProfile(app); appContext = app.run(args); appContext.registerShutdownHook(); 但是,如果应用程序在启动后被禁用,则不会调用@PreDestroy方法 import org.springframewor

根据Spring的文档,我添加了一个关机挂钩:

SpringApplication app = new SpringApplication(App.class);
DefaultProfileUtil.addDefaultProfile(app);
appContext = app.run(args);
appContext.registerShutdownHook();
但是,如果应用程序在启动后被禁用,则不会调用
@PreDestroy
方法

import org.springframework.stereotype.Service;
import javax.annotation.PreDestroy;
import javax.annotation.PostConstruct;
@Service
public class Processor {
  public Processor() {
    ...
  }
  @PostConstruct
  public void init() {
    System.err.println("processor started");
  }
  //not called reliably
  @PreDestroy
  public void shutdown() {
    System.err.println("starting shutdown");
    try {Thread.sleep(1000*10);} catch (InterruptedException e) {e.printStackTrace();}
    System.err.println("shutdown completed properly");
  }
}
我所看到的就是处理器启动了

processor started

^C
如果我等待spring至少30秒以完成启动,然后
kill
该过程,则会调用
@PreDestroy
注释函数

processor started
[...]
2018-12-26 17:01:09.050  INFO 31398 --- [  restartedMain] c.App                                    : Started App in 67.555 seconds (JVM running for 69.338)
2018-12-26 17:01:09.111  INFO 31398 --- [  restartedMain] c.App                                    :
----------------------------------------------------------
        Application 'App' is running! Access URLs:
        Local:          http://localhost:8081
        External:       http://10.10.7.29:8081
        Profile(s):     [dev]
----------------------------------------------------------
2018-12-26 17:01:09.111  INFO 31398 --- [  restartedMain] c.app                                    :
----------------------------------------------------------
^Cstarting shutdown
shutdown completed properly
我如何确定依赖于调用所有
@PreDestroy
注释函数的安全性

我知道如何在JVM中注册关机钩子,这就是我目前正在做的事情,但是在我看来,
@PreDestroy
应该这样做

通过“安全依赖”,我假设正常关机顺序(即由
SIGTERM
SIGINT
请求),而不是断电和终止进程等