【问题标题】:spring boot - load app config before calling method runspring boot - 在调用方法运行之前加载应用程序配置
【发布时间】:2018-09-24 14:39:21
【问题描述】:

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


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复,但我想要的是在执行方法运行之前阅读代码。我需要从函数 setMongoSecure() 中需要的 yaml 配置文件(application.yml)中加载一些参数
    • 您应该将初始化配置放在单独的属性文件中,然后通过调用 System.setProperty(...) 加载并设置它。
    猜你喜欢
    • 2018-07-21
    • 2019-12-04
    • 2020-03-15
    • 1970-01-01
    • 2021-11-16
    • 2020-10-03
    • 1970-01-01
    • 2019-06-28
    • 2017-02-24
    相关资源
    最近更新 更多