尝试在spring引导中使用属性文件

尝试在spring引导中使用属性文件,spring,spring-boot,Spring,Spring Boot,我有一个locations.properties文件,如下所示: location.endpoint=testadres,andertestadres location.test=teststring 我正在尝试使用以下类读取此配置文件: @Configuration @PropertySource("classpath:locations.properties") public class LocationProperties { @Value("#{'${location.endpoin

我有一个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();
}
}
@配置
@PropertySource(“类路径:locations.properties”)
公共类LocationProperties{
@值(“#{${location.endpoint}.split(',')}”)
私人名单地点;
@值(“${location.test}”)
私有字符串测试;
公共字符串getTest(){
返回此.test;
}
公共列表getLocations(){
将此文件返回到其他位置;
}
//在@Value中解析${}
@豆子
公共静态属性资源占位符配置器属性配置开发者(){
返回新属性资源占位符配置器();
}
}

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

只需将.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

只需将.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

尝试使用
“classpath*:locations.properties”
@prashantthakre,如果我这样做,我将获得:无法打开类路径资源[*/locations.properties],因为它没有打开exist@Philipe我刚刚修改了我的评论,你能试着使用我上面提到的通配符吗comments@prashantthakre还是不行。文件未找到错误。因此,使用上面的代码,文件已找到,值未被读取。我只是将您的代码复制到我的一个spring boot项目中,在这里工作正常。
propertyConfigInDev
bean似乎也不需要。你能粘贴用来调用
getTest
/
getLocations
的代码吗?尝试使用
“classpath*:locations.properties”
@prashantthakre如果我这样做,我会得到:类路径资源[*/locations.properties]无法打开,因为它没有打开exist@Philipe我刚刚修改了我的评论,你能试着使用我上面提到的通配符吗comments@prashantthakre还是不行。文件未找到错误。因此,使用上面的代码,文件已找到,值未被读取。我只是将您的代码复制到我的一个spring boot项目中,在这里工作正常。
propertyConfigInDev
bean似乎也不需要。能否粘贴用于调用
getTest
/
getLocations
的代码?