• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

springboot 搞代码 4年前 (2022-01-05) 68次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要介绍了springboot配置文件yml中的map形式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

springboot配置文件yml的map形式

1、yml中的格式

 t<b style="color:transparent">来源gao@!dai!ma.com搞$$代^@码网</b>es: maps: {key1: 12,key2: 34}

或者

 tes: maps: key1: 15 key2: 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 = {"classpath:/application.yml"}, encoding = "utf-8") //有的人是写了这个注解能成功,但是我这边不能有这个注解,有的话,就连编译都会报错 @ConfigurationProperties(prefix = "tes") public class MapTest { private Map 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) @SpringBootTest public class LzgApplicationTests { @Autowired private MapTest mapTest; @Test public void contextLoads() { System.out.println(mapTest.toString()); System.out.println("key1="+mapTest.getMaps().get("key1")); } } 

4、打印

 

SpringBoot yaml文件map集合使用

yaml文件配置

 patform.config: maps: person_one: userName: A platform: A platform person_two: userName: B platform: B platform 

配置文件对应的bean

如果yaml文件不是在application.yaml,则注解需要配置locations属性

 @ConfigurationProperties(value="platform.config",locations="classpath:config/applicaion-platform.yaml") public class ParamConfiguration{ private Map maps =new LinkedHashMap(); /** set ,get 方法  。。。。 */ public static class ParamInfo{ private String username; private String platform; /** set ,get 方法  。。。。 */ } }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持gaodaima搞代码网

以上就是使用springboot配置文件yml中的map形式的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用springboot配置文件yml中的map形式
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址