在春季启动中从文件系统加载properties.yaml文件

时间:2018-09-26 16:19:23

标签: spring spring-boot

使用Spring Boot从文件系统加载yaml文件时遇到麻烦。

我已将properties.yaml文件放在文件夹中

\config\source.properties
\config\config.yml

我正在用命令

调用jar
java -jar myjar.jar --spring.config.location=\config

我正在像这样加载属性文件,它工作正常。

@PropertySource("${spring.config.location}/source.properties")

但是下面的代码片段不会加载yaml属性

@Configuration
@ConfigurationProperties(prefix = "test-data")
@EnableConfigurationProperties
@Data
public class TestData {

    private Map<String, MyObject1> object1Map = new HashMap<>();

    private Map<Integer, MyObject2> object2Map= new HashMap<>();
}

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

请按照here所述,在位置参数的末尾给出一个'/'。

您提到的参数:seventymm将在此位置查找属性文件,并且必须以'/'结尾,否则您可以指定完整路径,包括文件名。或默认情况下,它将查找application.yml或application.properties。

以您为例,它必须尝试查找文件'config'

为了对属性使用其他名称,您需要使用名称进行指定。引用此'boot-features-external-config'