springBoot2.0 Yaml值获取
2024-09-03 17:33:35
1. pom.xml添加如下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
2. 编写bean文件
package cn.jfjb.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author john
* @date 2019/11/22 - 8:56
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private boolean boss;
private Date birth;
private List<Dog> dogs;
private Map<String, Object> maps;
private List<Object> lists;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public boolean isBoss() {
return boss;
}
public void setBoss(boolean boss) {
this.boss = boss;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public List<Dog> getDogs() {
return dogs;
}
public void setDogs(List<Dog> dogs) {
this.dogs = dogs;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", dogs=" + dogs +
", maps=" + maps +
", lists=" + lists +
'}';
}
}
3. 入口文件添加@EnableConfigurationProperties
package cn.jfjb.sbhelloworld01quick;
import cn.jfjb.bean.Person;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
@EnableConfigurationProperties({Person.class})
public class SbHelloworld01QuickApplication {
public static void main(String[] args) {
SpringApplication.run(SbHelloworld01QuickApplication.class, args);
}
}
4. 编写application.yaml文件
person:
last-name: "aa"
age: 12
boss: false
birth: 2019/11/11
dogs:
- {name: "aa",age: 11}
- {name: "bb",age: 12}
- {name: "cc",age: 14}
maps:
aa: 1
bb: "aas"
lists:
- 1
- 2
- 3
- 4
- 5
- 6
4. 进行测试
package cn.jfjb.sbhelloworld01quick;
import cn.jfjb.bean.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SbHelloworld01QuickApplicationTests {
@Autowired
Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
效果如图
最新文章
- 【转载】利用Unity自带的合图切割功能将合图切割成子图
- Java获取新浪微博cookies
- Python相对、绝对导入浅析
- Nodejs新建博客练习(二)添加flash支持
- 76. Minimum Window Substring
- html中a标签中的onclick和href的使用
- 该优化针对Linux X86_X64环境
- Javascript事件绑定及深入
- golang 互斥锁和读写锁
- selenium3+java+POM 跨浏览器测试之------读取配置文件
- MyBatis笔记----MyBatis查询表全部的两种方法:XML与注解
- [javascript]multipart/form-data上传格式表单自定义创建
- java 线程 (三)线程并发的安全性 同步代码块
- jquery将表单序列化json对象
- Comet OJ - Contest #2 简要题解
- DtCMS 在IIS7.0 下之伪静态
- Asp.Net Core App 部署故障示例 1
- 验证手机号码的JS方法
- luogu P1017 进制转换
- boost实用工具:typeof库 BOOST_TYPE BOOST_AUTO
热门文章
- asp.net 怎么上传文件夹啊,不传压缩包!
- css grid 随笔
- [JZOJ6241]【NOI2019模拟2019.6.29】字符串【数据结构】【字符串】
- pip安装源和虚拟环境的搭建
- install oh my zsh on ubuntu 16.04
- 不错的图表库:ChartDirector
- CodeForces 352C Jeff and Rounding
- jar 在windows 启动服务,卸载服务,停止端口
- Django JWT
- LeetCode 152. 乘积最大子序列(Maximum Product Subarray)