我是开发弹簧启动后端应用程序的团队的一员,该应用程序为移动应用程序提供数据。除了新功能之外,还需要在后端加载资源,这需要提供给移动应用程序。
由于这个资源很重要并且通过在启动期间加载一次来使资源访问有效,我想知道如果文件系统中没有此资源,我是否可以告诉spring boot无法启动。我知道注释@PostConstruct,但加载资源似乎为时已晚。
提前感谢suggtestions
答案 0 :(得分:3)
@PostConstruct可用于确保应用程序在启动时具有所需的内容,如DB池等。例如,如果文件不存在则可以抛出IllegalStateException(),并且可以阻止应用程序加载。我跑了一个快速测试,它的工作原理。您可以按照以下方式使用它们。
@PostConstruct
public void setup() {
// check the stuff that you need.
if (condition) {
throw new IllegalStateException();
}
}
答案 1 :(得分:1)
您可以在开始申请之前做到这一点。
@SpringBootApplication
public class SpringBootTestApplication {
public static void main(String[] args) {
// TODO Do your checking here and exit if you like
SpringApplication.run(SpringBootTestApplication, args);
}
}