基本特征

@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的格式
@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的区别的更多相关文章

  1. 转:web.xml 配置中classpath: 与classpath*:的区别

    原文链接:web.xml 配置中classpath: 与classpath*:的区别 引用自:http://blog.csdn.net/wxwzy738/article/details/1698393 ...

  2. web.xml 配置中classpath: 与classpath*:的区别

    首先 classpath是指 WEB-INF文件夹下的classes目录 解释classes含义: 1.存放各种资源配置文件 eg.init.properties log4j.properties s ...

  3. nginx配置中root与alias的区别

    nginx指定文件路径有两种方式root和alias,这两者的用法区别,使用方法总结了下,方便大家在应用过程中,快速响应.root与alias主要区别在于nginx如何解释location后面的uri ...

  4. 关于ehcache配置中timeToLiveSeconds和timeToIdleSeconds的区别

    在使用ehcache框架时,timeToLiveSeconds和timeToIdleSeconds这两个属性容易混淆,今天有空就记录一下,以防之后又忘记了. 首先来说明一下这两个属性分别有什么作用:( ...

  5. web.xml 配置中classpath: 与classpath*:的区别——(十一)

    首先 classpath是指 WEB-INF文件夹下的classes目录 解释classes含义: 1.存放各种资源配置文件 eg.init.properties log4j.properties s ...

  6. spring配置中classpath: 与classpath*:的区别

    classpath和classpath*区别:  classpath:只会到你的class路径中查找找文件. classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找 ...

  7. nginx配置中root和alias的区别

    例:访问http://127.0.0.1/download/*这个目录时候让他去/opt/app/code这个目录找. 方法一(使用root关键字): location / { root /usr/s ...

  8. springboot 配置 中查找application.properties中对应的数据,添加对应的prefix前缀

    @ConditionalOnProperty(prefix = "spring.redis", name = "enabled", havingValue = ...

  9. SpringBoot自定义属性配置以及@ConfigurationProperties注解与@Value注解区别

    我们可以在application.properties中配置自定义的属性值,为了获取这些值,我们可以使用spring提供的@value注解,还可以使用springboot提供的@Configurati ...

随机推荐

  1. Dubbo Filter详解

    转载:https://www.jianshu.com/p/c5ebe3e08161 Dubbo的Filter在使用的过程中是我们扩展最频繁的内容,而且Dubbo的很多特性实现也都离不开Filter的工 ...

  2. 使用visual studio code运行html

    可以安装插件:open in browser 安装好插件后,编辑html网页的时候,右键多了两个菜单: 1.Open in Default Browser 2.Open in Other Browse ...

  3. KMS服务器激活

    https://blog.csdn.net/weixin_42588262/article/details/81120403 http://kms.cangshui.net/ https://kms. ...

  4. javascript常用方法 - String

    // 1.长字符串 // 1.1 let longString1 = "This is a very long string which needs " + "to wr ...

  5. VMware设置桥接模式(使虚拟机拥有独立IP访问外网)

    1.关闭虚拟机里的系统 2.VMware主窗口 编辑---->虚拟网络编辑器 右下角----> 更改设置---->出现  桥接模式 桥接到:看本机所连接的网络, 网络属性中有一项“描 ...

  6. 转 zabbix 自动发现和 zabbix自定义用户key与参数User parameters

    ########31 https://www.cnblogs.com/yjt1993/p/10883345.html 1.概念 在配置Iterms的过程中,有时候需要对类似的Iterms进行添加,这些 ...

  7. CentOS7下Redis的安装与使用

    一.安装过程 1.准备工作(安装gcc依赖) # yum install gcc-c++ 2.下载并解压源码包 # cd /usr/local # wget http://download.redis ...

  8. node.js执行shell命令进行服务器重启

    nodejs功能强大且多样,不只是可以实现 服务器端 与 客户端 的实时通讯,另一个功能是用来执行shell命令 1.首先,引入子进程模块var process = require('child_pr ...

  9. sql server 2008 自动备份

    身份验证:包含Windows身份验证和 SQL Server身份验证,此处选择Windows 身份验证; 选择[管理]-->[维护计划]-->[维护计划向导] 必须启用代理服务(启动模式请 ...

  10. docker搭建samba共享目录

    需求:因同事需要共享文件夹来传输数据.整好接触docker,所以想用docker来搭建samber 系统:Centos7.4 docker搭建就不在赘述,如有需要请参考:https://www.jia ...