spring boot yaml 自定义配置 映射到 java POJO
只需要一个注解就ok:
@ConfigurationProperties("user.other")
“user.other” 这个值匹配的是user下的other对象
yaml :
yaml 的语法: https://yaml.org/spec/1.2/spec.html#directive//
user:
user-name: addiction
age:
friends:
- Smith
- Shadow
- Kathrin
other:
grand-test: test
color: colorful
price: '$223'
test:
-
user-name: addiction
age:
-
user-name: addiction
age:
-
user-other: addiction
age-other:
other:
test: "this is test"
nums:
-
-
-
UserProperty类:
其中的属性名要和yml一一对应, grandTest 在 yml 中对应的是 grand-test, 会自动转成驼峰
用 lombok 的 @Data 注解 生成getter/setter, 加上spring 的 @Component 方便 依赖注入
@Data
@Component
@ConfigurationProperties("user.other")
public class UserProperty {
private String grandTest;
private String color;
private String price; private List<Map<String, Object>> test; private Other other; //POJO 类
}
Other 类
@Data
public class Other { private String test; private List<Integer> nums;
}
测试结果:


测试基类
package com.example.demo; import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class BaseTest {
}
另外还可使用@Value注解修饰属性来获取yaml中的内容:
@Value("${user.other.color}")
private String color;
spring boot yaml 自定义配置 映射到 java POJO的更多相关文章
- 【Spring Boot】Spring Boot之自定义配置参数绑定到Java Bean
一.@Value方式 1.我的配置文件:application-dev.yml # 自定义项目配置 startproject: pro1: pro2: pro3: pro4: lists: - ' - ...
- spring boot使用自定义配置的线程池执行Async异步任务
一.增加配置属性类 package com.chhliu.springboot.async.configuration; import org.springframework.boot.context ...
- Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置
用过 Spring Boot 的小伙伴都知道,我们只需要在项目中引入 spring-boot-starter-web 依赖,SpringMVC 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...
- 自定义spring boot的自动配置
文章目录 添加Maven依赖 创建自定义 Auto-Configuration 添加Class Conditions 添加 bean Conditions Property Conditions Re ...
- Spring Boot 2.0 配置图文教程
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 ...
- 【转】spring boot application.properties 配置参数详情
multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold: 大于该值的文件会被写到磁盘上 multipart. ...
- Spring Boot 排除自动配置的 4 种方法,关键时刻很有用!
Spring Boot 提供的自动配置非常强大,某些情况下,自动配置的功能可能不符合我们的需求,需要我们自定义配置,这个时候就需要排除/禁用 Spring Boot 某些类的自动化配置了. 比如:数据 ...
- spring boot web相关配置
spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何web相关的配置便能提供web服务,这还得归于spri ...
- 转-spring boot web相关配置
spring boot web相关配置 80436 spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何w ...
随机推荐
- SQL Server权限设置
常用的几个系统表:(其实是View) sysdatabases,主数据库,服务器上的数据库 sysobjects,每个数据库,所有数据库对象 syscolumns,每个数据库,列 sysprocess ...
- Python-12-简单推导
列表推导:从其他列表创建列表 >>> [x * x for x in range(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] 下面实现只打印能 ...
- HDU 1027 G - Can you answer these queries?
http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Time Limit: 4000/2000 M ...
- yum指令之修复
折腾着搞 openvpn 网站服务器 yum指令 出了点问题 ------------------------------------------------------------ [root@cl ...
- 百度地图API的基本用法
首先 ,如果想调用百度地图api,你需要获取一个百度地图api的密钥. 申请秘钥的步骤: 1.搜索百度地图: 2.进入后,先登录然后点击申请密钥: 3. 4.申请成功,拥有密钥 有了密钥之后,引入百度 ...
- querySelector/querySelectorAll
querySelector获取页面I属性D为test的元素: document.getElementById("test"); //or document.querySelecto ...
- ABAP事件的简单用法
1.1.事件: 用于捕获某类对象状态的改变来触发事件的方法,并进行处理 1.2.定义:可以在类或接口中进行声明 EVENTS|CLASS-EVENTS evt EXPORTING … VALUE(p ...
- SQLAlchemy的基本使用
一.介绍 SQLAlchemy是一种ORM(Object-Relational Mapping)框架,用来将关系型数据库映射到对象上.该框架建立在DB API之上,将类和对象转化成SQL,然后使用AP ...
- 让您的 VS 2012/2013 升级开发 .NET 4.6 -- Targeting the .NET Framework 4.6 (多目标包)
原文出处:让您的 VS 2012/2013 升级开发 .NET 4.6 -- Targeting the .NET Framework 4.6 (多目标包) http://www.dotblogs.c ...
- Erlang程序设计(第2版)读书笔记(一)
正如<代码的未来>中所说,为了充分利用多核,并发变成将成为未来发展的趋势,对于并发编程的支持,Erlang确实是不二之选,Erlang在国内仍然较为小众,经典书籍相对也要少很多,最终选择了 ...