可以在启动时停止Spring Boot应用程序吗?
我的软件在启动时需要验证许可证。如果许可证不存在或无效,则该应用程序应关闭。实际的验证发生在@PostConstruct
中@Service
。
我目前的做法是:
if (!exist || !valid) {
System.exit(1);
}
但是,这种解决方案简直太可怕了。有没有可以在不调用REST端点的情况下停止spring boot应用程序的解决方案?
假设您的代码在组件中(或其他通过Spring检测到的bean)。您可以ApplicationContext
在退出调用之前简单地注入和close
。
public class LicenseChecker {
@Autowired
private ApplicationContext ctx;
public void check() {
if (!exist || !valid) {
((ConfigurableApplicationContext) ctx).close();
System.exit(1);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句