java - 如何在 Spring Boot Maven 项目中移动 application.properties 或执行外部属性
问题描述
所以我在 Spring Boot 中开发了一些应用程序,但我偶然发现了 application.properties 文件,我想让它更容易使用,因为我有一些环境要测试,比如 Dev、Uat 和 Production
当我想启动应用程序时,我使用 mvn clean install 和环境中的 application.properties 配置文件来构建它。导致我的应用程序仅将战争快照用于后端服务。
就像我想在开发中启动一样
spring.profiles.active=dev
或者当我想在我正在使用的 uat 环境中部署它时
spring.profiles.active=uat
有没有办法让它变得更容易,比如使用外部道具,所以我不再需要使用 application.properties,因为我会在我的每个环境中上传 application.properties,所以应用程序可以从外部属性中读取。
我已经尝试过 PropertySourcePlaceholderConfigurer,但没有运气
@Configuration
public class ExternalPropertyConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
final PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new Resource[ ] {
new FileSystemResource("/myth/app/data/weblogic_configuration/config/myth_uat_configuration.properties"),
new FileSystemResource("/myth/app/data/weblogic_configuration/config/myth_dev_configuration.properties"),
new FileSystemResource("/src/main/resources/myth_local_configuration.properties")
};
properties.setLocations( resources );
properties.setIgnoreResourceNotFound(true);
properties.setIgnoreUnresolvablePlaceholders(true);
return properties;
}
}
解决方案
我认为这就是您正在寻找的Spring boot YML doc。通常外部化配置是通过 ymls 实现的。
推荐阅读
- python - 类型错误:无法克隆对象'
' - firebase - 如何将 firebase 错误消息从终端显示到 UI?
- python - 在 Jupyter Notebook 中打印美容数据框
- android - 如何使这个 Firebase 存储函数返回字符串?
- modal-dialog - Livewire 如何设置 jetstream 对话框模式的宽度?
- docker - 为什么 jib dockerBuild 插件无法连接
- docker - 运行 docker pull 时如何使用不同的基本路径?
- security - 如何在.net web apis中存储密码等私人信息?
- flutter - 如何在 Flutter 应用程序中以表格格式显示 MySQL 数据库中的数据?
- node.js - Viber Node.JS Bot API - RichMediaMessage 对象 - 不起作用