使用springboot配置文件yml中的map形式

使用springboot配置文件yml中的map形式,博智网带你了解详细信息 。
【使用springboot配置文件yml中的map形式】目录

  • springboot配置文件yml的map形式
    • 1、yml中的格式
    • 2、创建一个类
    • 3、引用
    • 4、打印
  • SpringBoot yaml文件map集合使用
    • yaml文件配置
    • 配置文件对应的bean

springboot配置文件yml的map形式
1、yml中的格式tes:maps: {key1: 12,key2: 34}
或者
tes:maps:key1: 15key2: 2
2、创建一个类然后创建对应类型的字段(注意下这个类的那两个注释了的注解)
package com.etc.lzg;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;import java.util.Map;@Data@Component//@Configuration //这个我这里虽然存在时能成功,不过我注释了也是可以的,这个是看网上有人写就跟着写上的//@PropertySource(value = https://www.yf-zs.com/redian/{"classpath:/application.yml"}, encoding = "utf-8") //有的人是写了这个注解能成功,但是我这边不能有这个注解,有的话,就连编译都会报错@ConfigurationProperties(prefix = "tes")public class MapTest {private Map<String, String> maps;}
3、引用package com.etc.lzg;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class LzgApplicationTests {@Autowiredprivate MapTest mapTest;@Testpublic void contextLoads() {System.out.println(mapTest.toString());System.out.println("key1="+mapTest.getMaps().get("key1"));}}
4、打印
使用springboot配置文件yml中的map形式


使用springboot配置文件yml中的map形式



SpringBoot yaml文件map集合使用
yaml文件配置patform.config:maps:person_one:userName: Aplatform: A platformperson_two:userName: Bplatform: B platform
配置文件对应的bean如果yaml文件不是在application.yaml,则注解需要配置locations属性
@ConfigurationProperties(value="https://www.yf-zs.com/redian/platform.config",locations="classpath:config/applicaion-platform.yaml")public class ParamConfiguration{private Map<String,ParamInfo> maps =new LinkedHashMap<String,ParamInfo>();/**set ,get 方法。。。。*/public static class ParamInfo{private String username;private String platform;/**set ,get 方法。。。。*/}}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持趣讯吧 。

    推荐阅读