基本特征

@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. Spring的注解收集

    @Scope("prototype")spring 默认scope 是单例模式scope="prototype" 可以保证 当有请求的时候 都创建一个Actio ...

  2. grpc使用记录(一) gRPC编译(mscv/gcc)

    目录 1.编译前的准备工作 2.Windows下使用VS2019编译 2.1.使用cmake生成VS2019解决方案 2.2.使用msbuild工具进行编译 3.linux下编译 3.1 CentO ...

  3. centos7.5系统elasticsearch使用滚动和全新安装升级到最新的elasticsearch7.4.2版本

    背景: 生产环境大量使用 elasticsearch 集群,不同的业务使用不同版本的elasticsearch es经常曝出一些大的漏洞,需要进行版本升级,并且使用x-pack的基本验证功能,避免用户 ...

  4. Scheduling In Go

    https://www.ardanlabs.com/blog/2018/08/scheduling-in-go-part1.html https://blog.altoros.com/golang-i ...

  5. Consider defining a bean of type 'com.*.*.mapper.*.*Mapper' in your configuration.

    @Mapper 不能加载的问题 Consider defining a bean of type 'com.*.*.mapper.*.*Mapper' in your configuration. 添 ...

  6. WMS日常运维_WJC

    3.25.奥克斯项目Apache的server reached MaxClients setting问题 apachelog报错:[mpm_worker:error] [pid 2486:tid 14 ...

  7. LeetCode 112. Path Sum(路径和是否可为sum)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  8. Kafka高级设计和架构,一文深化理解

    主题: 1.kafka是写磁盘还是写内存? 2.kafka究竟是由 consumer 从 broker 那里拉数据,还是由 broker 将数据推到 consumer? 3.如何区分已消费(consu ...

  9. (转)搭建Elasticsearch和kibana环境

    搭建Elasticsearch和kibana环境 作者:IT云清 原文:https://blog.csdn.net/weixin_39800144/article/details/81162002 1 ...

  10. 安装 python 爬虫框架 Scrapy

    官方安装说明文档:https://doc.scrapy.org/en/latest/intro/install.html#installing-scrapy 一.scrapy 需要以下依赖 二.一般来 ...