我有一个Spring Web应用程序,正在尝试将YAML配置文件加载到Java配置类中。但是,在我的JUnit测试中实例化它之后,我的配置类仅包含空的成员变量。我是Spring的新手,可能错过了一些显而易见的事情。我使用Maven构建了项目,并具有Maven样式目录树。
我的配置Java类:
src / main / java / com / my / package / config / YAMLConfigDatabase:
package com.my.package;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "database")
public class YAMLConfigDatabase {
private String url;
private int port;
private String schema;
private String username;
private String password;
//Getters and setters are all here.
}
我的配置YAML文件:
src / main / resources / application.yml
server.port: 8090
database:
url: 'localhost'
port: 3306
schema: 'my_schema'
username: 'webappuser'
password: 'secretPassword'
我的JUnit测试检查我是否确实可以加载配置文件:
package com.my.package;
import com.my.package.config.YAMLConfigDatabase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {YAMLConfigDatabase.class})
public class YAMLConfigTest {
private YAMLConfigDatabase config;
@Autowired
public void setYAMLConfigDatabase(YAMLConfigDatabase config){
this.config = config;
}
@Test
public void isYAMLConfigLoaded(){
System.out.println(this.config);
System.out.println(this.config.getPassword());
//The above line returns "null", but I would like it to return "secretPassword".
}
}
编辑:
我将YAMLConfigDatabase.java
更改为如下形式:
package com.my.package.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "database")
@PropertySource(value = "classpath:application.yml") //new line
@Component
public class YAMLConfigDatabase {
@Value("${url}") //new line
private String url;
@Value("${port}") //new line
private Integer port;
@Value("${schema}") //new line
private String schema;
@Value("${username}") //new line
private String username;
@Value("${password}") //new line
private String password;
}
我使用了高级Promidor技巧将@Value注释添加到所有成员变量,并且还必须添加行@PropertySource(value = "classpath:application.yml")
。如果我跳过后面的步骤,则@Value批注中的参数将按字面意义进行解释,如我在评论中所述。
答案 0 :(得分:1)
由于我也为此付出了很多努力(@ConfigurationProperties
),因此我尝试使用一个简单的应用程序来测试您的代码;
这是一个版本为2.0.0的Spring Boot应用程序(我也在1.5.14中对其进行了测试)
application.yml:
custom:
var1: aaa
var2: bbb
var3: ccc
ConfigClass.java
@Configuration
@ConfigurationProperties(prefix = "custom")
public class ConfigFile {
private String var1;
private String var2;
private String var3;
public String getVar1() {
return var1;
}
public void setVar1(String var1) {
this.var1 = var1;
}
public String getVar2() {
return var2;
}
public void setVar2(String var2) {
this.var2 = var2;
}
public String getVar3() {
return var3;
}
public void setVar3(String var3) {
this.var3 = var3;
}
@Override
public String toString() {
return "ConfigFile{" +
"var1='" + var1 + '\'' +
", var2='" + var2 + '\'' +
", var3='" + var3 + '\'' +
'}';
}
}
和测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootAppTests {
@Autowired
ConfigFile configFile;
@Test
public void contextLoads() {
Assert.assertEquals(configFile.getVar1(), "aaa");
Assert.assertEquals(configFile.getVar2(), "bbb");
Assert.assertEquals(configFile.getVar3(), "ccc");
}
}
在测试结束时,它成功了所有案例。
我只是猜测;也许您应该删除一些已添加且不再需要的注释。