springboot获取配置文件中的变量(Springboot中yml配置文件使用Map格式)
springboot获取配置文件中的变量(Springboot中yml配置文件使用Map格式)
yml格式配置文件下面格式数据定义了2个App项目信息 ,key值分别为app1、app2。
demo:
client:
app1:
key: app001
secret: fcd64cea96f7f
url: https://www.baidu.com
app2:
key: app002
secret: a7a9bfd47c1adfa
url: http://www.qq.com
定义配置文件类
注意下面代码中的demo和client,demo为前缀,client为map数据根节点。该map有2个key
每个key对应的对象为一个AppItem对象。
@Configuration
@ConfigurationProperties(prefix = "demo")
@Setter
@Getter
public class YmlMapConfig {
Map<String AppItem> client;
public AppItem getByKey(String key){
return client.get(key);
}
}
@Getter
@Setter
public class AppItem {
private String key;
private String secret;
private String url;
}
查看效果