我是一名新手,正在学习Spring Boot。我有一个Spring Boot应用程序,似乎忽略了@PreDestroy注释——当我从命令行或通过Eclipse运行时,当应用程序关闭(例如通过ctrl-c)时,我从未看到@PreDestroy代码被运行。
以下是代码...
Application.java:
@SpringBootApplication
public class Application {
@Autowired
private MessageProcessor messageProcessor;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@PostConstruct
public void init() {
messageProcessor.run();
}
}
消息处理器配置:
@Configuration
public class MessageProcessorConfiguration {
@Bean
public MessageProcessor messageProcessor() {
return new MessageProcessorImpl();
}
}
消息处理器:
public class MessageProcessorImpl implements MessageProcessor {
@Override
public void run() {
while (isActive()) {
…
}
}
@PreDestroy
public void shutdown() {
System.out.println("MessageProcessorImpl - shutting down");
}
}