SpringBoot标签之@ConfigurationProperties、@PropertySource注解的使用
当获取主配置文件中属性值时,只需@ConfigurationProperties(prefix = "person")注解来修饰某类,其作用是告诉springBoot,此类中的属性将与默认的全局配置文件中对应属性一一绑定。属性名必须是application.yml或application.properties。【prefix = "person"】表示与配置文件中哪个层级的属性进行绑定。
当一些属性不想配置到主配置文件,需自定义一个配置文件,需通过@PropertySource注解指定此配置文件路径。
而@ConfigurationProperties(prefix = "xxx")注解指定自定义配置文件中哪个层级属性需绑定。
配置文件的位置:\src\main\resources\application.yml
一、bean类与主配置文件的绑定
案例:bean类Person.java
@Component
@ConfigurationProperties(prefix = "person")//只能从默认的全局文件中获取
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
//======================get 、set方法省略=============
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
配置文件:
person:
lastName: fang \n xin \n de
age: 18
boss: false
birth: 2018/12/10
maps: {a1: fang, a2: li,a3: zhang}
lists: [cat,pig,dog]
dog:
name: xiaogou10号
age: 1
测试案例:
package com.atguigu.springboot;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {
@Autowired
Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
打印结果:
Person{lastName='fang \n xin \n de', age=18, boss=false, birth=Mon Dec 10 00:00:00 CST 2018, maps={a1=fang, a2=li, a3=zhang}, lists=[cat, pig, dog], dog=Dog{name='xiaogou10号', age=1}}
二、bean类与自定义的配置文件绑定,需@ConfigurationProperties、@PropertySource两个注解一起修饰bean类
@PropertySource指定自定义配置文件的地址
@ConfigurationProperties指定绑定属性的层级
@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value ={"classpath:person.properties"})
@Validated
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
//==================set、get方法省略=======================
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
自定义配置文件person.properties:
person.lastName=李次方1110
person.age=12
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog01
person.dog.age=15
打印的结果:
Person{lastName='李次方1110', age=12, boss=false, birth=Fri Dec 15 00:00:00 CST 2017, maps={k1=v1, k2=14}, lists=[a, b, c], dog=Dog{name='dog01', age=15}}
三、@ConfigurationProperties注解的特点
1.批量注入配置文件中值
2.支持松散绑定
配置文件中属性格式分别为lastname 、last_name、last-name 时,都可以与Person类中属性lastName绑定
person:
lastname: fang \n xin \n de
3.支持JSR303校验:
Person类上加上@Validated,表示类中属性需要进行验证。 @Email表示lastName必须满足邮件格式
@Component
@ConfigurationProperties(prefix = "person")//只能从默认的全局文件中获取
@Validated
public class Person {
@Email
private String lastName;
}
配置文件中lastname值不是邮件格式
person:
lastname: fang \n xin \n de
输出person对应的实例时报错:
4.能够封装复杂结构的属性:例如:map集合、级联属性
5.不支持SpEL表达式
参考:SpringBoot之@ConfigurationProperties、@PropertySource注解的使用
SpringBoot标签之@ConfigurationProperties、@PropertySource注解的使用的更多相关文章
- springboot情操陶冶-@ConfigurationProperties注解解析
承接前文springboot情操陶冶-@Configuration注解解析,本文将在前文的基础上阐述@ConfigurationProperties注解的使用 @ConfigurationProper ...
- Springboot中PropertySource注解的使用
https://blog.csdn.net/qq_30739519/article/list/3 注解 https://blog.csdn.net/qq_30739519/article/detail ...
- springboot之使用@ConfigurationProperties注解
springboot之使用@ConfigurationProperties注解 代码已经上传至github https://github.com/gittorlight/springboot-exam ...
- SpringBoot加载配置文件(@PropertySource@importSource@Value)
情景描述 最近新搭建了一个项目,从Spring迁到了Springboot,为了兼容Spring加载配置文件的风格,所以还想把PropertyPlaceholderConfigurer放在.xml文件里 ...
- SpringBoot 标签之启动
在SpringBoot中入口我们使用: package com.sankuai.qcs.regulation.traffic; import org.springframework.boot.Spri ...
- JUnit5学习之五:标签(Tag)和自定义注解
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- springboot用@Autowired和@PostConstruct注解把config配置读取到bean变成静态方法
springboot用@Autowired和@PostConstruct注解把config配置读取到bean变成静态方法 @SpringBootApplication public class Sen ...
- springboot中@EnableAsync与@Async注解使用
springboot中@EnableAsync与@Async注解使用 @Async为异步注解,放到方法上,表示调用该方法的线程与此方法异步执行,需要配合@EnableAsync注解使用. 1.首先演示 ...
- Springboot中使用自定义参数注解获取 token 中用户数据
使用自定义参数注解获取 token 中User数据 使用背景 在springboot项目开发中需要从token中获取用户信息时通常的方式要经历几个步骤 拦截器中截获token TokenUtil工具类 ...
随机推荐
- Caused by: java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp
错误信息如下: Caused by: java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java ...
- python:while循环、运算符、初始编码
while循环 while -- 关键字 while 条件: 缩进代码块 以上循环是(死循环) 终止循环的方法 1.break 跳出循环,并且把循环给干掉了 2.continue 跳出本次循环,继续下 ...
- RestFul风格API(Swagger)--从零开始Swagger
引言:随着技术的革新,现在的系统基本上都是前后端分离,并且在各自的道路上越走越远,而前后端之间通信或者联系的桥梁就是API,而这里基于RESTful风格的API框架就来了!欲知后事如何,客官别急,往下 ...
- spring-boot项目的新建(出生)
了解了SSM之后,发现是真的强大,但是配置有点多,是不是啊,老铁,所以Spring-boot被我给抓住,了解了一波.[/斜眼笑] 一.Spring-boot是啥呢? 以我现在的水平,无法用花里胡哨的语 ...
- web 项目:解决插入 MySQL 数据库时中文乱码问题
背景:在做 javaweb 项目的时,前台传递的中文最后插入数据库的时候总是出现乱码现象. 解决方案 A.不管是使用 Idea.eclipse,确定自己的项目所使用的字符集是 UTF-8. B ...
- ODOO(ERP源码安装)
cat /etc/centos-release CentOS Linux release 7.4.1708 (Core) uname -r 3.10.0-693.el7.x86_64 IP:192.1 ...
- Kano模型告诉你“是不是只要企业努力的提高产品或服务质量,顾客满意度就一定会提高吗?”
在 PO在敏捷需求下要遵守哪6条重要原则? 中讲到探索和交付两大阶段的6个原则 其中在原则[做有价值的需求]中讲了两个方法:BSA分析和产品Backlog 其中BSA分析是我在产品规划阶段常使用的 ...
- Materialized View模式
Materialized-View模式是在要求数据格式不利于查询操作的情况下,根据多个数据仓库的数据生成预生成的视图的一种模式.这种模式可以帮助支持高效的查询和数据提取,提高应用程序的性能. 问题 在 ...
- 谈谈自己体会到的HTML5
# 谈谈自己体会到的HTML5 很多介绍HTML5的文章,都是从以下几个方面去介绍的:语义化.丰富的表单.本地存储.多媒体.地理位置等等.纸上得来终觉浅,现在有了一定的实践经验之后,本人对其有了更加深 ...
- 纯手写AJAX
function ajax(){ //http相应对象 var xmlhttp; //判断浏览器 if(window.XMLHttpRequest){ xmlhttp = new XMLHttpReq ...