java - 如何在 Spring Boot Maven 项目中移动 application.properties 或执行外部属性

标签 java spring spring-boot maven

所以我在 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.propertiesapplication-prod.properties

它们会自动加载。不需要代码来加载它们

你可以看看这个Spring Boot Features

关于java - 如何在 Spring Boot Maven 项目中移动 application.properties 或执行外部属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62316247/

相关文章:

java - 为什么我在使用 Quartz job Scheduler 时在 contextInitialized() 方法中遇到空指针异常

java - 通过检查字典进行 Spring 验证

spring-boot - Spring Boot 应用程序与 Spring Cloud 的集成测试

java - Virtuoso Jena 程序中的 Reasoner

java - 如何通过在文本字段中输入编号来创建数组,Java 小程序

java - 具有多个参数的 Mockito 参数匹配器

java - Spring 2.1.0.M4rabbitmq声明队列并在运行时将它们绑定(bind)到监听器

java - Spring可以处理POJO吗?

java - Spring 集成注释 Java 8 可选的不正确处理

java - log4j 无法在 Spring Boot Rest api 中创建日志文件