gpt4 book ai didi

java - 我应该能够将 yaml 与 spring ConfigurationProperties 一起使用吗?

转载 作者:行者123 更新时间:2023-11-30 07:25:26 25 4
gpt4 key购买 nike

我正在尝试使用 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=测试值

application.yml:

pre:
testBeanValue: test-value

这里是a link to the full sample

最佳答案

您将 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 {
}

关于java - 我应该能够将 yaml 与 spring ConfigurationProperties 一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36874197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com