尝试在spring boot

时间:2015-07-06 15:56:28

标签: spring spring-boot

我有一个locations.properties文件,如下所示:

location.endpoint=testadres,andertestadres
location.test=teststring

我正在尝试使用以下类读取此配置文件:

@Configuration
@PropertySource("classpath:locations.properties")
public class LocationProperties {

@Value("#{'${location.endpoint}'.split(',')}")
private List<String> locations;

@Value("${location.test}")
private String test;

public String getTest() {
    return this.test;
}

public List<String> getLocations() {
    return this.locations;
}

//To resolve ${} in @Value
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
    return new PropertySourcesPlaceholderConfigurer();
}
}

但是每当我尝试使用getLocations()或getTest()来获取字符串时,我都会得到null?

1 个答案:

答案 0 :(得分:0)

只需将.properties或.yml文件添加到/ resources文件夹即可。实施例

# simple.yml
demo:
connection:
    username: sample
    sample: My text

并添加POJO类

@Component
@ConfigurationProperties(
prefix = "demo.connection", 
locations = "classpath:sample.yml"
)
public class SampleSettings {

private String sample;

private String username;

..Getters and Setters mandatory