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 ...
随机推荐
- Maven配置及使用总结
一. 安装Maven 1. Maven官网 http://maven.apache.org/ 2. 本例子下载最新的版本,apache-maven-3.3.9 解压后目录描述: bin 含有maven ...
- VUE图片懒加载-vue lazyload插件的简单上手使用(优化版本)
在用VUE做项目开发的过程中,首页用到了懒加载的方法,查找了一些方法,觉得官网写得太复杂,有一篇博客不错(https://www.cnblogs.com/xyyt/p/7650539.html),但是 ...
- Golang 入门系列(十三)用Beego开发web应用
接着之前的内容,前面已经讲过很多Golang的基础语法,mysql的使用,redis的使用,也讲了orm框架,如何创建一个webapi 服务等等,感兴趣的可以看看以前的文章,https://www.c ...
- windows环境下memcache配置方法 详细篇
将memcache服务器安装包解压到C:\memcached文件夹后,使用cmd命令窗口安装. 1>开始>运行:CMD(确定) 2>cd C:\memcached(回车) 3> ...
- 【转】"超时时间已到。在操作完成之前超时时间已过或服务器未响应"的解决方法
方法有以下三种: 1.原因应该在数据访问有问题,可以把连接时间设置长些,在数据库连接字符串,加上Connect Timeout=18000,单位毫秒 2,在web.config中加上以下语句: < ...
- 求一个极大数的欧拉函数 phi(i)
思路: 因为当n>=1e10的时候,线性筛就不好使啦.所以要用一个公式 φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn) 证明详见:<公式 ...
- Java技术面试汇总
1.servlet执行流程 客户端发出http请求,web服务器将请求转发到servlet容器,servlet容器解析url并根据web.xml找到相对应的servlet,并将request.resp ...
- Spring 设计原则
Spring 框架有四大原则(Spring所有的功能和设计和实现都基于四大原则): 1. 使用POJO进行轻量级和最小侵入式开发. 2. 通过依赖注入和基本接口编程实现松耦合. 3. 通过AOP和基于 ...
- iOS 最新判断机型设备方法
#define isIphoneXXS [UIScreen mainScreen].bounds.size.width == 375 && [UIScreen mainScreen] ...
- 对Yii 2.0模型rules的理解(load()无法正确装载数据)
在实际开发中,遇到数据表新增字段而忘记了在对应模型中rules规则中添加新增的字段,而导致load()方法装载不到新增字段,导致新增字段无法写入数据库中. 解决办法:在新增字段后及时在对应模型ru ...