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 ...
随机推荐
- java中HashSet对象内的元素的hashCode值不能变化
因为不管是HashMap(或HashTable,还是HashSet),key值是以hashCode值存进去的,加入key值变了,将无法从集合内删除对象,导致内存溢出.
- rabbitMQ消息队列 – Message方法解析
消息的创建由AMQPMessage对象来创建$message = new AMQPMessage("消息内容");是不是很简单. 后边是一个数组.可以对消息进行一些特殊配置$mes ...
- 用GEOquery从GEO数据库下载数据--转载
https://www.plob.org/article/9969.html Gene Expression Omnibus database (GEO)是由NCBI负责维护的一个数据库,设计初衷是为 ...
- (转)2019年 React 新手学习指南 – 从 React 学习线路图说开去
原文:https://www.html.cn/archives/10111 注:本文根据 React 开发者学习线路图(2018) 结构编写了很多新手如何学习 React 的建议.2019 年有标题党 ...
- C++ vector使用实例
C++ vector #include <iostream> #include <vector> #include <string> #include <al ...
- 泡泡一分钟:Optimal Trajectory Generation for Quadrotor Teach-And-Repeat
张宁 Optimal Trajectory Generation for Quadrotor Teach-And-Repeat链接:https://pan.baidu.com/s/1x0CmuOXiL ...
- python工程设置工具(pipenv)
原始安装 pip工具 --- 包安装工具, 可以从Python包索引hub上安装,也可以使用自定义的hub. 命令: pip install xxx 缺点: 1.命令方式, 一次只能安装一个包, 对于 ...
- WPF--控件模板的视觉效果呈现流程及逻辑
外部通过属性把数据--传递给-->(破拆后)内部可视化树 ----> 内部可视化树呈现出视觉效果 ----> 各种内部可视化组件的视觉效果组合 --- 呈现 --> 外部的 ...
- 开启和安装Kubernetes k8s 基于Docker For Windows
0.最近发现,Docker For Windows Stable在Enable Kubernetes这个问题上是有Bug的,建议切换到Edge版本,并且采用下文AliyunContainerServi ...
- Visual Studio 2017 Add WSDL
Normal way Right click Project -> Add -> Web Reference -> Advanced Intranet way download ws ...