SpringBoot中的@ConfigurationProperties
有什么用
该注解的作用类似于@Value,即为组件中的属性进行赋值。
怎么用
首先是建立一个springboot的工程,不再赘述。
首先我们建立一个Person类和一个Dog类。
package com.helius.springboot.bean;
@Component //加上它,即成为ioc容器中的一个组件
@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;
// 省略了setter、getter、toString()等
}
再来看SpringBoot的主配置文件application.properties
person.last-name=张三
person.age=18
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=perter
person.dog.age=15
在springboot的测试类中进行测试
/**
* SpringBoot单元测试;
*
* 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {
@Autowired
Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
控制台打印一下结果:
Person{lastName='张三', age=18, boss=false, birth=Fri Dec 15 00:00:00 CST 2017, maps={k1=v1, k2=14}, lists=[a, b, c], dog=Dog{name='perter', age=15}}
我们发现,容器中person组件已经全部被赋值了。
解释
因为我们使用了@ConfigurationProperties(prefix = "person")这个注解
将配置文件中配置的每一个属性的值,映射到这个组件中
@ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定; prefix = "person":配置文件中哪个下面的所有属性进行一一映射
相关注解
在之前的博文中:Spring中使用@Value和@PropertySource为属性赋值中介绍@Value注解,
它能够为组件注入外部配置文件中的值。
那在springboot中的,可以直接使用@Value注解,不需要导入主配置文件application.properties,毕竟它是主配置文件,也是放在springboot约定的位置的。
既然@Value和@ConfigurationProperties都能为组件赋值,那区别呢?
| @ConfigurationProperties | @Value | |
|---|---|---|
| 功能 | 批量注入配置文件中的属性 | 一个个指定 |
| 松散语法 | 支持 | 不支持 |
| SpEL | 不支持 | 支持 |
| JSR303数据校验 | 支持 | 不支持 |
| 复杂类型封装 | 支持 | 不支持 |
- 松散语法:可以发现配置文件中我们使用
person.last-name也能够为Person类中的lastName属性进行赋值。
其实你使用last-name还是last_name亦或lastName都能够为Pseron中对应的属性赋值,这叫松散绑定
- @Value支持Spel这个可以在之前的博文中看到 ,而 @ConfigurationProperties 是不支持的。
- 我们在springmvc中使用过@Validated这个注解。在@ConfigurationProperties也是支持的,使用示例放在最后面
- 最后一点,@Value不能够对复杂类型进行赋值,如private Map<String,Object> maps;该属性,就无法在属性加上
@Vavlue(${person.map})是拿不到值的。
@Validated应用举例
改造一下Person类。
package com.helius.springboot.bean;
@Component //加上它,即成为ioc容器中的一个组件
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
@Email
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
// 省略了setter、getter、toString()等
}
这是在赋值的时候检查lastName必须是邮件格式,
重新运行测试方法
Field error in object 'person' on field 'lastName': rejected value [张三]; codes [Email.person.lastName,Email.lastName,Email.java.lang.String,Email]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [person.lastName,lastName]; arguments []; default message [lastName],[Ljavax.validation.constraints.Pattern$Flag;@702ed190,org.springframework.validation.beanvalidation.SpringValidatorAdapter$ResolvableAttribute@173b9122]; default message [不是一个合法的电子邮件地址]
SpringBoot中的@ConfigurationProperties的更多相关文章
- SpringBoot中使用@ConfigurationProperties提示:Configuration Annotation Processor not found in classpath
问题 Springboot1.5以上版本,在使用 @ConfigurationProperties注解的时候会提示Spring Boot Configuration Annotation Proces ...
- Spring-boot中@ConfigurationProperties,@Value,@PropertySource
1.利用@ConfigurationProperties获取配置的值,@ConfigurationProperties是springboot提供的基于安全类型的配置放置. application.pr ...
- SpringBoot中yaml配置对象
转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...
- SpringBoot(五):@ConfigurationProperties配置参数绑定
在springmvc或其他ssh框架中如果我们要实现一个配置参数的加载,需要使用代码实现读取properties文件等操作,或者需要使用其他属性@value(name="username&q ...
- springboot中实现多数据源
springboot中实现多数据源 1.什么场景需要多数据源 业务读写分离 业务分库 业务功能模块拆分多库 2.常见的多数据源的方案 按照数据源分别把mapper和entity放到不同的package ...
- springboot情操陶冶-@ConfigurationProperties注解解析
承接前文springboot情操陶冶-@Configuration注解解析,本文将在前文的基础上阐述@ConfigurationProperties注解的使用 @ConfigurationProper ...
- springBoot中实现自定义属性配置、实现异步调用、多环境配置
springBoot中其他相关: 1:springBoot中自定义参数: 1-1.自定义属性配置: 在application.properties中除了可以修改默认配置,我们还可以在这配置自定义的属性 ...
- redis-手写redis切片和非切片连接池并注入springboot中
spring-data整合了redispool, 并提供redisTemplate使用, 但有时需要用到shradedJedisPool, 就需要手动注入了 手写redispool并注入springb ...
- springboot中的常用注解
springboot中的常用注解个人觉得springboor中常用的注解主要可以分为三种:放入容器型注解.从容器中取出型注解和功能型注解.其中的放入容器型和从容器中取出型就是我们平时所说的控制反转和依 ...
随机推荐
- Centos7.6安装docker-compose
官网地址:https://docs.docker.com/compose/install/ 运行此命令以下载Docker Compose的当前稳定版本 sudo curl -L "https ...
- 对象key值排序,以key值(数字)大小顺序遍历属性,helper._sort()
var helper = { _sort:function(data){ //{“20141216”:{},“20141217”:{}}按大小排序, var arr1 = [],arr2=[]; fo ...
- strcspn函数的用法
#include <string.h> main() { char *str = "Linux was first developed for 386/486-based pcs ...
- [SPOJ] DIVCNT2 - Counting Divisors (square) (平方的约数个数前缀和 容斥 卡常)
题目 vjudge URL:Counting Divisors (square) Let σ0(n)\sigma_0(n)σ0(n) be the number of positive diviso ...
- (尚024)Vue_案例_交互删除
注意:本总结中最终会删除不成功 ,原因是Item.vue中方法methods单词拼写错误!!! 首先明白,删除在Item.vue中交互 1.写交互,首先写监听@click="deleteIt ...
- (尚023)Vue_案例_交互添加
最终达到效果: 1.做交互,首先需要确定操作哪个组件? 提交------操作组件Add.vue 2.从哪开始做起呢? 从绑定事件监听开始做起,确定你跟谁绑定事件监听,在回调函数中做什么, ====== ...
- linux命令之------More命令
More命令 1)作用:命令类似cat,不过会以一页一页的形式显示,更方便使用者逐页阅读. 2)-num:一次显示的行数 3)-d:提示使用者,在画面下方显示[Press space to conti ...
- SQL基础-更新&删除&视图
一.更新数据 1.更新数据 ### 更新全部数据: 使用UPDATE关键字.语法如下: UPDATE 表名 SET 字段名=新的值; 比如: 更新学生表中的所有学生性别为男: UPDATE stude ...
- kafka 创建消费者报错
kafka-console-consumer.sh --zookeeper master:2181,slave1:2181,slave2:2181 --topic test --from-beginn ...
- hbase 监控指标项
名词解释 JMX:Java Management Extensions,用于用于Java程序扩展监控和管理项 GC:Garbage Collection,垃圾收集,垃圾回收机制 指标项来源 主机名 u ...