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工具类 ...
随机推荐
- BZOJ2124:等差子序列(线段树,hash)
Description 给一个1到N的排列{Ai},询问是否存在1<=p1<p2<p3<p4<p5<…<pLen<=N (Len>=3), 使得A ...
- Python 字典一个易犯的错误
一个易犯的错误,关于 Python 的传值(对于不可变量) 和 传引用(对于可变量),浅拷贝和深拷贝.废话不多说,看例子, 直接改变可变字典值,失败, >>> dic = dict. ...
- 浅析mysql中exists 与 in 的使用
一.exists的使用 exists对外表用loop逐条查询,每次查询都会查看exists的条件语句,当exists里的条件语句能够返回记录行时(无论记录行是的多少,只要能返回),条件就为真,返 ...
- 首页技术支持常见问题宽带外网IP显示为10、100、172开头,没有公网IP,如何解决?
1.表现形式: 路由器拨号获得的公网IP变成了一个以100开头的IP(或者是10.172开头),而打开ip138.com查询却又是另外一个IP,将100开头的这个IP到百度去查询下则显示所在区域为美国 ...
- Recurrent Neural Network[Quasi RNN]
0.背景 RNN模型,特别是包含着门控制的如LSTM等模型,近年来成了深度学习解决序列任务的标准结构.RNN层不但可以解决变长输入的问题,还能通过多层堆叠来增加网络的深度,提升表征能力和提升准确度.然 ...
- git 忽略无效解决办法
有时候发现git提交了一些我们不需要提交的内容,这时候第一反应是加个忽略: https://github.com/github/gitignore 宇宙神器VS: https://github.com ...
- Arduino Core For ESP8266
如果选择纯C作为ESP8266的开发,有两个途径: 使用乐鑫官方原生的 RTOS-SDK或者NONOS-SDK 使用Arduino IDE 使用PlatformIO 作为一个"Arduino ...
- Arduino通过MAX9814实现录音
如果通过Arduino进行录音不是单纯地接一个驻极电容MIC就可以的,因为自然界中的声音非常复杂,波形极其复杂,通常我们采用的是脉冲代码调制编码.即PCM编码.PCM通过抽样.量化.编码三个步骤将连续 ...
- 数组去重--ES5和ES6
思路:把去重后的数组放到一个空数组中 ES5实现: function uni(arr) { var result = []; for (var i=0;i<arr.length;i++) { i ...
- Node 系列之path模块
//引用该模块 var path = require("path"); 1.路径解析,得到规范化的路径格式 //对window系统,目录分隔为'\', 对于UNIX系统,分隔符为' ...