我应该能够在Spring ConfigurationProperties中使用yaml吗?

时间:2016-04-26 19:35:01

标签: java spring spring-boot configuration-files

我试图使用Spring的很酷的ConfigurationProperties功能来自动将配置值加载到bean中。我已经能够使用属性文件,但是当我使用yaml文件尝试相同的东西时,它不起作用。

我已经看过类似的例子,但它似乎并不适合我:

TestBean.java:

package com.kerz;

public class TestBean {
  private String value;

  public String getValue() {
    return value;
  }

  public void setValue(String value) {
    this.value = value;
  }

  public TestBean(String value) {
    this.value = value;
  }
}

JavaTestConfiguration.java:

package com.kerz;

import org.springframework.context.annotation.Configuration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;

import javax.validation.constraints.NotNull;

@Configuration
@ConfigurationProperties(prefix="pre")
public class JavaTestConfiguration {

  @NotNull
  String testBeanValue;

  public String getTestBeanValue() {
    return testBeanValue;
  }

  public void setTestBeanValue(String testBeanValue) {
    this.testBeanValue = testBeanValue;
  }

  @Bean
  TestBean testBean() {
    return new TestBean(testBeanValue);
  }
}

JavaTestConfigurationTest.java:

package com.kerz;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertEquals;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {JavaTestConfiguration.class})
//@PropertySource("classpath:application.properties")
@PropertySource("classpath:application.yml")
@EnableConfigurationProperties({JavaTestConfiguration.class})
public class JavaTestConfigurationTest {

  @Autowired
  TestBean testBean;

  @Test
  public void shouldWork() throws Exception {
    assertEquals("testBean", "test-value", testBean.getValue());
  }
}

application.properties:

pre.testBeanValue=test-value

application.yml:

pre:
  testBeanValue: test-value

这里是a link to the full sample

1 个答案:

答案 0 :(得分:1)

您正在将Configuration课程与Properties课程混合,这是一项更难设置的工作。修复它的最简单方法是将它们拆分为:

@ConfigurationProperties(prefix="pre")
public class JavaTestConfigurationProperties {

  @NotNull
  String testBeanValue;

  public String getTestBeanValue() {
    return testBeanValue;
  }

  public void setTestBeanValue(String testBeanValue) {
    this.testBeanValue = testBeanValue;
  }
}

@Configuration
@EnableConfigurationProperties({JavaTestConfigurationProperties.class})
public class JavaTestConfiguration {
  @Bean
  TestBean testBean(JavaTestConfigurationProperties properties) {
    return new TestBean(properties.getTestBeanValue());
  }
}

然后使用它,即:

@ContextConfiguration(classes = {JavaTestConfiguration.class})
public class JavaTestConfigurationTest {
}