spring boot 读取配置文件(application.yml)中的属性值
在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值:
1、引入依赖:
- <!-- 支持 @ConfigurationProperties 注解 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
- </dependency>
2、配置文件(application.yml)中配置各个属性的值:
- myProps: #自定义的属性和值
- simpleProp: simplePropValue
- arrayProps: 1,2,3,4,5
- listProp1:
- - name: abc
- value: abcValue
- - name: efg
- value: efgValue
- listProp2:
- - config2Value1
- - config2Vavlue2
- mapProps:
- key1: value1
- key2: value2
3、创建一个bean来接收配置信息:
- @Component
- @ConfigurationProperties(prefix="myProps") //接收application.yml中的myProps下面的属性
- public class MyProps {
- private String simpleProp;
- private String[] arrayProps;
- private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1里面的属性值
- private List<String> listProp2 = new ArrayList<>(); //接收prop2里面的属性值
- private Map<String, String> mapProps = new HashMap<>(); //接收prop1里面的属性值
- public String getSimpleProp() {
- return simpleProp;
- }
- //String类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要
- public void setSimpleProp(String simpleProp) {
- this.simpleProp = simpleProp;
- }
- public List<Map<String, String>> getListProp1() {
- return listProp1;
- }
- public List<String> getListProp2() {
- return listProp2;
- }
- public String[] getArrayProps() {
- return arrayProps;
- }
- public void setArrayProps(String[] arrayProps) {
- this.arrayProps = arrayProps;
- }
- public Map<String, String> getMapProps() {
- return mapProps;
- }
- public void setMapProps(Map<String, String> mapProps) {
- this.mapProps = mapProps;
- }
- }
启动后,这个bean里面的属性就会自动接收配置的值了。
4、单元测试用例:
- @Autowired
- private MyProps myProps;
- @Test
- public void propsTest() throws JsonProcessingException {
- System.out.println("simpleProp: " + myProps.getSimpleProp());
- System.out.println("arrayProps: " + objectMapper.writeValueAsString(myProps.getArrayProps()));
- System.out.println("listProp1: " + objectMapper.writeValueAsString(myProps.getListProp1()));
- System.out.println("listProp2: " + objectMapper.writeValueAsString(myProps.getListProp2()));
- System.out.println("mapProps: " + objectMapper.writeValueAsString(myProps.getMapProps()));
- }
测试结果:
- simpleProp: simplePropValue
- arrayProps: ["1","2","3","4","5"]
- listProp1: [{"name":"abc","value":"abcValue"},{"name":"efg","value":"efgValue"}]
- listProp2: ["config2Value1","config2Vavlue2"]
- mapProps: {"key1":"value1","key2":"value2"}
spring boot 读取配置文件(application.yml)中的属性值的更多相关文章
- Spring Boot 的配置文件application.properties
Spring Boot 中的application.properties 是一个全局的配置文件,放在src/main/resources 目录下或者类路径的/config下. 作为全局配置文件的app ...
- 一:Spring Boot 的配置文件 application.properties
Spring Boot 的配置文件 application.properties 1.位置问题 2.普通的属性注入 3.类型安全的属性注入 1.位置问题 当我们创建一个 Spring Boot 工程时 ...
- 是时候搞清楚 Spring Boot 的配置文件 application.properties 了!
在 Spring Boot 中,配置文件有两种不同的格式,一个是 properties ,另一个是 yaml . 虽然 properties 文件比较常见,但是相对于 properties 而言,ya ...
- spring boot读取配置文件
一.springboot配置文件 核心配置文件和自定义配置文件.核心配置文件是指在resources根目录下的application.properties或application.yml配置文 ...
- 第二篇:彻底搞清楚 Spring Boot 的配置文件 application.properties
前言 在Spring Boot中,配置文件有两种不同的格式,一个是properties,另一个是yaml. 虽然properties文件比较常见,但是相对于properties而言,yaml更加简洁明 ...
- IDEA开发spring boot应用时 application.yml 或 application.properties 自定义属性提示
在使用spring boot开发过程中,经常会定义一些应用自己的属性,直接写到application配置文件中使用@Value注解进行使用,这样使用也没有什么问题.不过我认为更优雅的方式是定义自己的属 ...
- spring boot 读取配置文件信息
1.读取application.properties @Component @ConfigurationProperties(prefix="images.product.column&qu ...
- Spring Boot读取配置文件的几种方式
Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口.这三种注解可以配合着@PropertySou ...
- springboot工程读取配置文件application.yml的写法
现在流行springboot框架的项目,里面的默认配置文件为application.yml,我们怎样读取这个配置文件呢? 先贴上我得配置文件吧 目录结构 里面内容 1 写读取配置文件的工具类 @Con ...
随机推荐
- 监视网络接口TCP状态信息数据有多种工具或命令。下面举例一些:
nstat命令 nstat kernel ======= ss -s == netstat -i netstat -s ip -s link sar -n DEV 1
- Callable、Future、FutureTask_笔记
参考:http://blog.csdn.net/javazejian/article/details/50896505 1.Callable<V>接口 Runnable接口 public ...
- 【bzoj1822】[JSOI2010]Frozen Nova 冷冻波 计算几何+二分+网络流最大流
题目描述 WJJ喜欢“魔兽争霸”这个游戏.在游戏中,巫妖是一种强大的英雄,它的技能Frozen Nova每次可以杀死一个小精灵.我们认为,巫妖和小精灵都可以看成是平面上的点. 当巫妖和小精灵之间的直线 ...
- SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)
COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from ...
- [luoguP1975] [国家集训队]排队(分块)
传送门 直接暴力分块,然后在每一个块内排序. 查询时可以在每一个块内二分. #include <cmath> #include <cstdio> #include <io ...
- location.origin兼容
if (!window.location.origin) { window.location.origin = window.location.protocol + "//" + ...
- pat 甲级 1080. Graduate Admission (30)
1080. Graduate Admission (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- Bzoj1195 [HNOI2006]最短母串 [状态压缩]
Time Limit: 10 Sec Memory Limit: 32 MBSubmit: 1304 Solved: 439 Description 给定n个字符串(S1,S2,„,Sn),要求找 ...
- 【CF1015F】Bracket Substring(字符串DP)
题意:给定一个只由左右括号组成的字符串s,问长度为2*n的包含它的合法括号序列方案数,答案对1e9+7取模 1≤n≤100,1≤|s|≤200 思路:暴力预处理出s的每个前缀[0..i]后加左右括号分 ...
- my conclusion about time planing and requirement changes.
How to estimate time when requirement changes or is added: my principle: never say so easy, at least ...