Spring Boot-在调用方法运行之前加载应用程序配置

时间:2018-09-24 14:39:22

标签: spring-boot yaml

有一种方法可以在启动应用程序之前(更确切地说,在调用方法运行之前)读取Spring Boot应用程序的yaml文件。

这是我的代码:

@SpringBootApplication
public class CometeRestApi extends SpringBootServletInitializer {

private static void setMongoSecure() {
    System.setProperty("javax.net.ssl.keyStore", System.getProperty("user.home") + "/.ssl/client-and-key.jks");
    System.setProperty("javax.net.ssl.keyStorePassword", "");

    System.setProperty("javax.net.ssl.trustStore", System.getProperty("user.home") + "/.ssl/certificateChain.jks");
    System.setProperty("javax.net.ssl.trustPassword", "");
}

@Override
protected SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
    setMongoSecure();
    return application.sources(CometeRestApi.class);
}

public static void main(final String[] args) {
    setMongoSecure();
    SpringApplication.run(CometeRestApi.class, args);
}

}

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

是的,有几种方法可以实现,但是推荐的方法是使用EventListener。当您的目的是在应用程序启动后自动执行代码时,用@EventListener(ApplicationReadyEvent.class)注释方法非常理想。