我有一个带有以下 application.yml 的 Spring Boot 应用程序
Detail_1:
name: X,Y,Z
place: xplace,yplace,zplace
Detail_2:
name: X,Y,Z
place: xplaceanother,yplaceanother,zplaceanother
如何在java中获取此 map :
X { detail1 :xplace detail2 :xplaceanother } Y { detail1:yplace, detail2:yplaceanother } Z{ detail1:zplace, detail2:zplaceanother }
我尝试过以下代码:
@Value${detail1.name}
private String names;
@value${detail2.place}
List<Object> Names = Arrays.asList(getNames().split(","));
List<Object> places = Arrays.asList(getPlaces().split(","));
然后我尝试创建与细节 1 相对应的名称和地点的 map 同样,我获取了详细信息 2 的名称和地点 但在这种情况下,我最终得到 2 张 map ,一张用于细节 1,一张用于细节 2。 我需要创建一张 map 。
最佳答案
您需要使用@ConfigurationProperties
注释
以下 URL 提供了 .properties
和 .yml
格式的良好示例:
https://www.mkyong.com/spring-boot/spring-boot-configurationproperties-example/
https://www.baeldung.com/configuration-properties-in-spring-boot
关于java - Spring从yml文件加载Map值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57268811/