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 ...
随机推荐
- day4列表作业详解
1.day4题目 day4作业 1,写代码,有如下列表,按照要求实现每一个功能 li = ["alex", "WuSir", "ritian" ...
- ASP.NET Core Linux
环境说明 CentOS / 7.1 (64bit) (Linux操作系统) 3MySQL5.7(网站应用数据库) .NET Core SDK 2.0.0(网站应用环境) Nginx(反向代理服务器) ...
- DotNetAnywhere
DotNetAnywhere:可供选择的 .NET 运行时 原文 : DotNetAnywhere: An Alternative .NET Runtime作者 : Matt Warren译者 : ...
- NET Core项目部署
NET Core项目部署到linux(Centos7) 阅读目录 1.开篇说明 2.Jexus简单说明 3.Visual Studio 2015本地发布并且测试 4.配置Jexus并且部署.NET C ...
- Python 踩坑之旅进程篇其四一次性踩透 uid euid suid gid egid sgid的坑坑洼洼
目录 1.1 踩坑案例 1.2 填坑解法 1.3 坑位分析 1.4 技术关键字 1.5 坑后思考 下期坑位预告 代码示例支持 平台: Centos 6.3 Python: 2.7.14 代码示例: 菜 ...
- nginx的配置文件server_name的意义 location意义
配置不同的域名 不同域名都可以有首地址 location 同一域名下 分发到不同的路径 或者项目
- C#实现程序单例日志输出
对于一个完整的程序系统,一个日志记录是必不可少的.可以用它来记录程序在运行过程中的运行状态和报错信息.比如,那些不想通过弹框提示的错误,程序执行过程中捕获的异常等. 首先,在你的解决方案中,适当的目录 ...
- Memcached分布式原理
http://younglibin.iteye.com/blog/2043761 浅显易懂,值得一读
- css3背景与边框相关样式
background-attachment 背景图像是否固定或者随着页面的其余部分滚动 background-color 设置元素的背景颜色 b ...
- hystrix 给方法加断路器
添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>s ...