spring-boot - 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);
}
}
提前感谢您的帮助
解决方案
是的,有几种方法可以做到这一点,但推荐的方法是使用 EventListener。@EventListener(ApplicationReadyEvent.class)
当您的目的是在应用程序启动后自动执行代码时,使用注释方法是理想的。
推荐阅读
- java - 访问映射的 URL 时出现 Spring Rest 错误 404
- amazon-web-services - 使用 AWS CLI 调整 Redshift 集群大小的问题
- ios - 实施自定义 JWT 工作流
- flutter - 如何在 Flutter 中实现应用栏操作文本而不是操作图标?
- node.js - 所需的 Mongoose 验证子文档不起作用
- microsoft-graph-api - Microsoft Graph API - 合同 - 如何提取订阅的 sku?
- django - Django:Haystack 和 Elasticsearch 与 Mongoengine
- r - 在同一轴刻度上绘制多个图
- c# - C#中的代码完整短键
- datetime - 按最晚日期排序的存储过程