SpringBoot配置中@ConfigurationProperties和@Value的区别
基本特征
@ConfigurationProperties
- 与@Bean结合为属性赋值
- 与@PropertySource(只能用于properties文件)结合读取指定文件
- 与@Validation结合,支持JSR303进行配置文件值的校验,如@NotNull@Email等
@Value
- 为单个属性赋值
- 支持属性上的SpEL表达式
两者比较
@ConfigurationProperties | @Value | |
功能 | 批量注入配置文件中的属性 | 一个个指定 |
松散绑定 | 支持 | 不支持 |
SpEL | 不支持 | 支持 |
JSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |
我们用简单的例子来说明一下。
假设在application.properties文件中这样写道:
student.name=zhangsan
student.age=
student.class=mba
student.squad-leader=false
student.mail=zhangsan@gmail.com student.maps.k1=aaa
student.maps.k2=bbb
student.maps.k3=ccc student.lists=a,b,c student.score.english=
student.score.math=
分别用上面两种绑定属性的方式写两个bean:
StudentCP类使用@ConfigurationProperties的方式来绑定属性,相关比较内容可以看代码上面的注释。
@Component
// @PropertySource表示加载指定文件
// @PropertySource(value = {"classpath:student.properties"})
@ConfigurationProperties(prefix = "student")
// prefiex表示指定统一前缀,下面就不用再写了
@Validated // ConfigurationProperties形式下支持JSR303校验
public class StudentCP { private String name; private Integer age; // 支持松散绑定,可以将连接符转成驼峰命名
private Boolean squadLeader; // 当前形式下支持JSR303数据校验,表示此属性值必须是email的格式
private String mail; // 支持复杂类型封装对应
private Map<String, Object> maps; private List<Object> lists; private Score score; }
StudentV类使用@Value的方式来绑定属性,注释中给出了简单的说明。
public class StudentV { // 使用@Value的话只能给属性一一指定映射 @Value("student.name")
private String name; // @Value形式支持SpEL表达式
@Value("#{13*2}")
// @Value("student.age")
private Integer age; // @Value("true") // 可直接赋值
// 不能支持松散语法的绑定
@Value("student.squad-leader")
private Boolean squadLeader; @Value("student.mail")
private String mail; // 之后的map、list和对象等复杂形式对象@Value无法支持 }
小结
配置文件格式是yml或者properties两者都能够获取值;
如果说只想在某个业务逻辑中获取一下配置文件中的某个值,使用@Value;
如果说专门编写一个JavaBean来和配置文件进行映射,那么就使用@ConfigurationProperties.
其他
@ConfigurationProperties默认从全局配置文件中获取值,如果要从指定配置文件中获取值,那么需要通过@PropertySource来声明指定。
@PropertySource(value = "classpath:student.properties")
@ImportResource:导入Spring的配置文件,让配置文件里面的内容生效。
我们自定义的配置文件,默认是不能注入(加载)到Spring的IoC容器中的,如果想在项目中使用自定义的配置文件,则需要通过@ImportResource来指定注入才能够最终使用。
@ImportResource(location = {"classpath:my-beans.xml"})
以前的项目中,我们都在xml中配置bean,但是实际当中(目前),我们不会使用这种方式来给项目添加组件,SpringBoot有推荐的方式,即使用注解。
@Configuration标注一个类,指明当前类是一个配置类,就是用来替代之前Spring使用xml配置的方式。(<bean id="" class=""></bean>)
@Configuration
public class MyConfig { /**
* 将方法的返回值注入到容器中,容器中这个组件默认的id就是方法名
*/
@Bean
public HelloService helloService() {
return new HelloService();
}
}
配置文件占位符
可以在***.properties中使用占位符使用一些函数,或者调用之前配置的一些内容:
${ramdom.value}
${random.int(10)}
${student.name}
// 获取student.hobit的值,没有的话取冒号后面的缺省值
${student.hobit:football}
SpringBoot配置中@ConfigurationProperties和@Value的区别的更多相关文章
- 转:web.xml 配置中classpath: 与classpath*:的区别
原文链接:web.xml 配置中classpath: 与classpath*:的区别 引用自:http://blog.csdn.net/wxwzy738/article/details/1698393 ...
- web.xml 配置中classpath: 与classpath*:的区别
首先 classpath是指 WEB-INF文件夹下的classes目录 解释classes含义: 1.存放各种资源配置文件 eg.init.properties log4j.properties s ...
- nginx配置中root与alias的区别
nginx指定文件路径有两种方式root和alias,这两者的用法区别,使用方法总结了下,方便大家在应用过程中,快速响应.root与alias主要区别在于nginx如何解释location后面的uri ...
- 关于ehcache配置中timeToLiveSeconds和timeToIdleSeconds的区别
在使用ehcache框架时,timeToLiveSeconds和timeToIdleSeconds这两个属性容易混淆,今天有空就记录一下,以防之后又忘记了. 首先来说明一下这两个属性分别有什么作用:( ...
- web.xml 配置中classpath: 与classpath*:的区别——(十一)
首先 classpath是指 WEB-INF文件夹下的classes目录 解释classes含义: 1.存放各种资源配置文件 eg.init.properties log4j.properties s ...
- spring配置中classpath: 与classpath*:的区别
classpath和classpath*区别: classpath:只会到你的class路径中查找找文件. classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找 ...
- nginx配置中root和alias的区别
例:访问http://127.0.0.1/download/*这个目录时候让他去/opt/app/code这个目录找. 方法一(使用root关键字): location / { root /usr/s ...
- springboot 配置 中查找application.properties中对应的数据,添加对应的prefix前缀
@ConditionalOnProperty(prefix = "spring.redis", name = "enabled", havingValue = ...
- SpringBoot自定义属性配置以及@ConfigurationProperties注解与@Value注解区别
我们可以在application.properties中配置自定义的属性值,为了获取这些值,我们可以使用spring提供的@value注解,还可以使用springboot提供的@Configurati ...
随机推荐
- php如何实现三级分销
Q: 项目要实现三级分销;对于数据库的设计和用户注册后给所有上级(最多三级)返利 但是一点头绪都没有,请大神帮忙给个思路! 如果是直接注册给奖励20元如果是通过二维码或者链接进入的注册页面 找到上级 ...
- Log4j 1.x版 引发线程blocked死锁问题(2008)
1. https://blog.csdn.net/zl378837964/article/details/84884934 2. 去掉debug
- Ubuntu 14.04下超级终端Minicom连接ARM(转)
转自:https://blog.csdn.net/ajianyingxiaoqinghan/article/details/70209765 笔者的工作环境: PC系统:Ubuntu 14.04 LT ...
- 往hbase插入数据,你会选择哪种?
好久,好久没有写个博客了,自从上次封闭开始,到“自闭”,有了一段时间了,哈哈^_^ . 多亏了云桌面的歇菜, 一下午啥都干不了, 突然想到,好久没有写点啥了,就写的,让时间流走有点痕迹吧 _(:з」∠ ...
- 解决docx4j 变量替换 由于变量存在样式式或空白字符 导致替换失败问题
参考文章:https://blog.csdn.net/qq_35598240/article/details/84439929 使用docx4j进行变量替换时 变量(形如:${变量})必须是无格式的, ...
- springboot项目使用 apollo 配置中心
1. 引入 apollo 配置依赖 <dependency> <groupId>com.ctrip.framework.apollo</groupId> <a ...
- chmod: changing permissions of ‘/etc/fstab': Read-only file system
给passwd文件加权限,修改/etc/fstab目录下所有的文件夹属性为可写可读可执行,执行以下命令:chomd 777 /etc/fstab 的时候提示错误: chmod: changing pe ...
- IfcBeam属性
IfcBeam属性和结构 <xs:element name="IfcBeam" type="ifc:IfcBeam" substitutionGroup= ...
- 带有Q_OBJECT的类要放在头文件的第一个类位置,否则可能无法moc
如果头文件中有多个类,带有Q_OBJECT的类要放在头文件的第一个类位置,否则可能无法moc
- java 迭代的陷阱
/** * 关于迭代器,有一种常见的误用,就是在迭代的中间调用容器的删除方法. * 但运行时会抛出异常:java.util.ConcurrentModificationException * * 发生 ...