Java 通过spring.profiles.active覆盖默认应用程序属性

Java 通过spring.profiles.active覆盖默认应用程序属性,java,spring,spring-boot,properties,spring-autoconfiguration,Java,Spring,Spring Boot,Properties,Spring Autoconfiguration,通过spring.profiles.active属性指定活动概要文件,覆盖默认应用程序属性时遇到问题 我开发了一个示例项目来复制这个问题。 我有2个application.yml文件: application-default.yml spring: profiles: active: local test: value: "This is the test value for application properties of default" appli

通过
spring.profiles.active
属性指定活动概要文件,覆盖默认应用程序属性时遇到问题

我开发了一个示例项目来复制这个问题。 我有2个application.yml文件:

application-default.yml

spring:
  profiles:
    active: local

test:
  value: "This is the test value for application properties of default"
application-local.yml

test:
  value: "This is the test value for application properties of local"
然后,我开发了一个组件来打印测试值:

@Component
public class TestComponent {

    @Value("${test.value}")
    private String testValue;

    @PostConstruct
    public void postConstruct() {

        System.out.println(testValue);
    }
}
此代码的输出为:

2021-01-07 16:19:01.080  INFO 335505 --- [           main] com.test.Application                     : The following profiles are active: local
This is the test value for application properties of default
2021-01-07 16:19:01.419  INFO 335505 --- [           main] com.test.Application                     : Started Application in 0.944 seconds (JVM running for 1.441)
我预计产出将是:

This is the test value for application properties of local
我知道使用
application.yml
而不是
application default.yml
可以正常工作,但我的目标是使用
application.yml
来保存所有概要文件的公共基本属性,并通过使用概要文件指定特定于环境的配置

我正在使用SpringBoot2.4.0,
build.gradle

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter', version: '2.4.0'
}

为什么不重写此属性?

这是由于spring boot加载属性的顺序造成的。 使用活动配置文件和所有配置文件的通用属性创建application.yml文件,并从
application default.yml
中删除spring.profiles.active属性

spring:
  profiles:
    active: local
您还可以像这样在运行时选择活动概要文件

java -jar example.jar --spring.profiles.active=local

这是由于spring boot加载属性的顺序造成的。 使用活动配置文件和所有配置文件的通用属性创建application.yml文件,并从
application default.yml
中删除spring.profiles.active属性

spring:
  profiles:
    active: local
您还可以像这样在运行时选择活动概要文件

java -jar example.jar --spring.profiles.active=local

默认属性文件应称为application.yml,而不是application-default.yml

spring:
  profiles:
    active: local

test:
  value: "This is the test value for application properties of default"
只要试着重新命名它,它就会工作得很好。。在这里阅读更多


默认属性文件应称为application.yml,而不是application-default.yml

spring:
  profiles:
    active: local

test:
  value: "This is the test value for application properties of default"
只要试着重新命名它,它就会工作得很好。。在这里阅读更多


是的,我知道
application.yml
可以按预期工作,尽管这不是我想要的。我的目标是使用
application.yml
作为每个概要文件的基本属性文件,并使用概要文件的属性指定特定于环境的配置。这正是它的工作方式。它为您传递的任何配置文件设置application.yml中的所有属性,但如果您在application-profile.properties中定义任何内容并使用该配置文件,它将使用您在该配置文件中定义的内容覆盖它们。因此,在我的应用程序中,我有application.properties(而不是yml)它具有所有通用配置,例如,日志级别设置为INFO,然后是application-dev.properties,其中我将日志级别设置为TRACE,application-uat.properties,其中我将日志级别设置为DEBUG。在运行spring boot应用程序时,我将-Dspring.profiles.active=dev作为命令行参数传递。是的,我知道
application.yml
按预期工作,尽管这不是我想要的。我的目标是使用
application.yml
作为每个概要文件的基本属性文件,并使用概要文件的属性指定特定于环境的配置。这正是它的工作方式。它为您传递的任何配置文件设置application.yml中的所有属性,但如果您在application-profile.properties中定义任何内容并使用该配置文件,它将使用您在该配置文件中定义的内容覆盖它们。因此,在我的应用程序中,我有application.properties(而不是yml)它具有所有通用配置,例如,日志级别设置为INFO,然后是application-dev.properties,其中我将日志级别设置为TRACE,application-uat.properties,其中我将日志级别设置为DEBUG。在运行spring boot应用程序时,我将-Dspring.profiles.active=dev作为命令行参数传递。按命令行传递活动概要文件的工作方式与预期的一样,但为了让开发人员更舒适,我希望在不使用该参数的情况下解决此问题。是的,我知道,
application.yml
按预期工作,尽管这不是我想要的。我的目标是使用
application.yml
作为每个配置文件的基本属性文件,并使用配置文件的属性来指定特定于环境的配置。通过命令行传递活动配置文件可以按预期的方式工作,但为了让开发人员更舒适,我希望在不使用该文件的情况下解决此问题。是的,我知道,
application.yml
按预期工作,尽管这不是我想要的。我的目标是使用
application.yml
作为每个概要文件的基本属性文件,并使用概要文件的属性指定特定于环境的配置。