@ConfigurationProperties @value
功能 批量注入配置文件中的属性 一个个指定
松散绑定(松散语法) 支持 不支持
SpEL 不支持 支持
JSR303数据校验 支持 不支持
复杂类型封装 支持 不支持

所谓松散语法也就是属性命名规则(Relaxed binding)

- person.firstName:使用标准方式
- person.first-name:大写用-
- person.first_name:大写用_
- PERSON_FIRST_NAME: 系统属性推荐使用这种写法

@ConfigurationProperties支持松散语法         @Value不支持松散语法

application.properties文件中有字段 persion.first-name

@Component
@ConfigurationProperties(prefix = "person")
public class Person { private String firstName;
}

JSR303数据校验(@Validation)

@ConfigurationProperties @Validated 支持      @Value不支持

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Email;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated; @ConfigurationProperties
@Validated
public class Properties { @NotNull
private String userName; @Email
private String email; }

SpEl

@ConfigurationProperties不支持EL表达式

application.properties文件中有字段 person.userAge=12

userAge = 12   //可以
userAge = #{2*6}  //EL表达式不支持

@Value支持EL表达式

@Value(userAge=#{2*6}) //支持EL表达式

 

使用原则:在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;如果专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;

springboot @value和@configurationproperties注解的区别的更多相关文章

  1. springboot之使用@ConfigurationProperties注解

    springboot之使用@ConfigurationProperties注解 代码已经上传至github https://github.com/gittorlight/springboot-exam ...

  2. springboot情操陶冶-@ConfigurationProperties注解解析

    承接前文springboot情操陶冶-@Configuration注解解析,本文将在前文的基础上阐述@ConfigurationProperties注解的使用 @ConfigurationProper ...

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

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

  4. SpringBoot标签之@ConfigurationProperties、@PropertySource注解的使用

    当获取主配置文件中属性值时,只需@ConfigurationProperties(prefix = "person")注解来修饰某类,其作用是告诉springBoot,此类中的属性 ...

  5. SpringBoot 配置 @ConfigurationProperties 与 @Value 区别

    一.SpringBoot 配置 @ConfigurationProperties 与 @Value 区别 配置文件 yml 还是 properties 他们都能获取到值: 如果说,我们只是在某个业务逻 ...

  6. SpringBoot#ConfigurationProperties注解相关的一些知识

    用途:ConfigurationProperties注解,用于在spring环境定义bean的时候.通过这个注解,把配置文件中的相关属性注入到实例化的bean中. 原理:spring中bean的生命周 ...

  7. springboot中的常用注解

    springboot中的常用注解个人觉得springboor中常用的注解主要可以分为三种:放入容器型注解.从容器中取出型注解和功能型注解.其中的放入容器型和从容器中取出型就是我们平时所说的控制反转和依 ...

  8. @ConfigurationProperties与@value区别

    @ConfigurationProperties与@value区别   @ConfigurationProperties @value 功能 批量注入配置文件中的属性 一个个指定 松散绑定 支持 不支 ...

  9. @ConfigurationProperties 注解使用姿势,这一篇就够了

    在编写项目代码时,我们要求更灵活的配置,更好的模块化整合.在 Spring Boot 项目中,为满足以上要求,我们将大量的参数配置在 application.properties 或 applicat ...

随机推荐

  1. How to fix "http error 403.14 - forbidden" in IIS7

    If you encounter the following error: "http error 403.14 - forbidden. The Web server is configu ...

  2. android Activity中设置setResult 没有触发onActivityResult

    昨天修改<manifest 文件中activity 的 模式为单例模式:android:launchMode="singleTask" ,发现我的onActivityResu ...

  3. Android多线程操作sqlite(Sqlite解决database locked问题)

    参考http://blog.csdn.net/sdsxleon/article/details/18259973  很好 https://github.com/2point0/Android-Data ...

  4. Hdu1695 GCD 2017-06-27 22:19 30人阅读 评论(0) 收藏

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  5. Windows 8.1 Update中的小改变

    在Build 2014大会中,发布了Windows 8.1 Update的更新,并将于4月8日通过Windows Update进行推送.但是,在MSDN订阅下载中,带有该更新的镜像已经可以在4月3号放 ...

  6. hihocoder 二分

    题目 一个简单的二分,只是想说明一下,如若要查找一个数组中某个数的下标可以直接用lower_bound()这个函数.只是要考虑到要查找的数不在数组中的这种情况. #include <cstdio ...

  7. 转:Ubuntu 10.10 安装后上不了网的原因

    最近新装了个Ubuntu10.10 发现上不了网,折腾了很久,在网上找了很多办法都不行,最后试了一招居然管用了.特此总结下Ubuntu了网的原因及对策分析. 环境:Ubuntu 10.10网络: 通过 ...

  8. 学习JavaScript计划

    1.首先根据视频做小例子 2.每天记录到博客 3.这次坚持把这个学完,并完成接口测试界面的编写

  9. B - Big String

    We will construct an infinitely long string from two short strings: A = "^__^" (four chara ...

  10. Python 学习第二章

    本章内容 数据类型 数据运算 表达式 if ...else 语句 表达式 for 循环 表达式 while 循环 一.数据类型 在内存中存储的数据可以有多种类型. 在 Python 有五个标准的数据类 ...