所以我在 Spring Boot 中开发一些应用程序,但我偶然发现了 application.properties 文件,我想让它更容易使用,因为我有一些环境需要测试,比如 Dev、Uat 和 Production
当我想启动应用程序时,我使用 mvn clean install 和环境中的 application.properties 配置文件来构建它。导致我的应用程序仅使用 war 快照作为后端服务。
就像当我想在开发中启动时
spring.profiles.active=dev
或者当我想将其部署在我正在使用的uat环境中时
spring.profiles.active=uat
有没有办法让它变得更容易,比如使用外部 Prop ,所以我不再需要使用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;
}
}
最佳答案
构建 jar 后,您可以像这样启动它
java -jar -Dspring.profiles.active=dev "app.jar"
提供 Activity 配置文件将使该特定配置文件生效
您只需创建配置文件范围的属性文件,例如 application-dev.properties
或 application-prod.properties
它们会自动加载。不需要代码来加载它们
你可以看看这个Spring Boot Features
关于java - 如何在 Spring Boot Maven 项目中移动 application.properties 或执行外部属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62316247/