Java 我应该能够将yaml与spring配置属性一起使用吗?

Java 我应该能够将yaml与spring配置属性一起使用吗?,java,spring,spring-boot,configuration-files,Java,Spring,Spring Boot,Configuration Files,我正试图使用spring的cool ConfigurationProperties特性将配置值自动加载到bean中。我已经能够让它与属性文件一起工作,但是当我尝试与yaml文件进行相同的操作时,它不起作用 我看到过类似的例子,但似乎对我不起作用: TestBean.java: package com.kerz; public class TestBean { private String value; public String getValue() { return val

我正试图使用spring的cool 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;
  }
}
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);
  }
}
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());
  }
}
pre:
  testBeanValue: test-value
JavaTestConfiguration.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;
  }
}
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);
  }
}
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());
  }
}
pre:
  testBeanValue: test-value
JavaTestConfigurationTest.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;
  }
}
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);
  }
}
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());
  }
}
pre:
  testBeanValue: test-value
应用程序。属性:

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;
  }
}
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);
  }
}
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());
  }
}
pre:
  testBeanValue: test-value
testBeanValue之前=测试值

application.yml:

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;
  }
}
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);
  }
}
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());
  }
}
pre:
  testBeanValue: test-value

这里是您将
配置
类与
属性
类混合在一起,这是一个更难实现的设置。解决此问题的最简单方法是将其拆分为:

@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 {
}

我可以看到将配置和属性分离的组织吸引力,但我看到yaml道具没有被拾取的相同行为…?这是因为只有属性被拾取,而不是yml文件吗?您有application.properties和application.yml文件配置相同的属性。其中一个必须获胜。你试过YAML文件吗?thx@AndyWilkinson,一次只能使用其中一个。注意我是如何注释掉上面的
@PropertySource
行的?ConfigurationProperties(我认为配置文件也是YAML)是Spring引导的一部分,而不是Spring框架本身。但是,在测试中不使用任何Spring引导注释。我不确定它是否打算在非Spring引导环境中工作。也许安迪可以对此发表评论。我希望在您的测试中使用
@SpringApplicationConfiguration(classes=Application.class)
注释,并将Application.class作为Spring Boot应用程序类,它可以工作。@dunni,我正在使用来自Spring Boot的
@EnableConfigurationProperties
,它看起来应该可以工作,但您永远不知道(没有深入研究源代码:)