首页 > 解决方案 > 如何在 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;
    }
}

标签: javaspringspring-bootmaven

解决方案


我认为这就是您正在寻找的Spring boot YML doc。通常外部化配置是通过 ymls 实现的。


推荐阅读