【问题标题】:Spring Boot YAML profiles configuration fileSpring Boot YAML 配置文件配置文件
【发布时间】:2020-05-07 06:55:36
【问题描述】:

我正在尝试使用 Spring Boot 测试 YAML 配置文件。以下yaml-props.yml是我的配置文件:

name: def
---
spring:
   profiles: live
   name: live
---
spring:
   profiles: test
   name: test

另外,我在application.properties 文件中将spring.profiles.active 属性设置为live (两个配置文件都在src/test/resources 文件夹下。) 最后,这是我的测试类:

@SpringBootTest
@TestPropertySource("classpath:yaml-props.yml")
public class YMLUnitTest {

    @Autowired
    private YMLProps ymlProps;

    @Autowired
    private Environment env;

    @Test
    public void testYmlProps() {

        assertEquals("live", env.getActiveProfiles()[0]); // Active profile is 'live'
        assertEquals("live", ymlProps.getName()); // Fails here!
    }
}

但是,在第二个断言中测试失败:

org.opentest4j.AssertionFailedError: expected: <live> but was: <test>

Spring 似乎选择了最后定义的配置文件。为什么会这样?

【问题讨论】:

  • 你能分享你的pom.xml文件和YMLProps吗?

标签: spring-boot yaml


【解决方案1】:

首先你的配置文件有误(注意缩进),应该是:

name: def
---
spring:
  profiles: live
name: live
---
spring:
  profiles: test
name: test

第二个问题是,你不能在任何地方都使用 YAML 文件,在那里你可以使用属性文件,@TestPropertySource 就是这种情况,你不能简单地使用 YAML 文件...

如果您使用application.yml,这很容易,但可能会令人困惑。如果你想使用多个 YAML 文件,我建议你使用 -Dspring.config.additional-location=classpath:yaml-props.yml

这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>betlista</groupId>
    <artifactId>springTests-springBootTest</artifactId>
    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.2.5.RELEASE</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

YMLProps 类

package betlista.springTests.springBootTest;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties
public class YMLProps {

    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

YMLUnitTest 类

package betlista.springTests.springBootTest;

import betlista.springTests.springBootTest.YMLProps;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test,live")
public class YMLUnitTest {

    @Autowired
    private YMLProps ymlProps;

    @Autowired
    private Environment env;

    @Test
    public void testYmlProps() {
        String[] activeProfiles = env.getActiveProfiles();
        Assert.assertEquals("live", activeProfiles[1]); // Active profile is 'live'
        Assert.assertEquals("live", ymlProps.getName()); // Fails here!
    }

}

全部可在 GitHub 上找到:https://github.com/Betlista/SpringTests/tree/master/YMLProps

【讨论】:

    【解决方案2】:

    尝试在@SpringBootTest之后添加以下内容。

    @SpringBootTest(value={"spring.profiles.active=live"})
    

    我希望它有效!

    【讨论】:

    • 您可以尝试根据配置文件将不常见的属性拆分为两个不同的文件,例如application-test.ymlapplication-live.yml 然后我认为这个属性会起作用。
    【解决方案3】:

    来自 Spring 文档

    YAML 文档按照遇到的顺序合并。后面的值会覆盖前面的值。

    您应该控制要使用的配置文件,即--spring.profiles.active=live

    【讨论】:

    • 我在问题中提到我设置了spring.profiles.active 属性。
    猜你喜欢
    • 2016-02-05
    • 2020-01-23
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 2019-03-08
    相关资源
    最近更新 更多