首页 > 解决方案 > spring boot - 在调用方法运行之前加载应用程序配置

问题描述

有没有办法在启动应用程序之前读取 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);
}

}

提前感谢您的帮助

标签: spring-bootyaml

解决方案


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


推荐阅读