基本特征

@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. Three.js 快速上手以及在 React 中运用[转]

    https://juejin.im/post/5ca22692f265da30a53d6656 github 的地址 欢迎 star! 之前项目中用到了 3D 模型演示的问题,整理了一下之前学习总结以 ...

  2. Spring Boot-Error:(3, 32) java: 程序包org.springframework.boot不存在

    问题分析 -由于加载的项目没有加载相应的依赖的包文件导致 解决方案 setting 选中图中的设置,点击apply,IDE就会自动下载所需要的包文件

  3. intellij idea远程调试

    有时候发布后的包不得不进行debug,但是又不方便本地开发环境直接debug模拟,所以不得不需要远程debug. 启动参数 首先在服务端使用JVM的-Xdebug参数启动Jar包. java -Xde ...

  4. Docker学习大纲

    Docker学习大纲:https://www.cnblogs.com/CloudMan6/p/7637361.html

  5. 行车记录仪 MyCar Recorder (转)

    行车记录仪 MyCar Recorder

  6. 解决在SQLPLUS中无法使用方向键、退格键问题

    1. wget ftp://ftp.pbone.net/mirror/ftp5.gwdg.de/pub/opensuse/repositories/home:/matthewdva:/build:/E ...

  7. centos7.5环境下elasticserch5.6.15集群升级6.8.4

    节点的角色分片: node01 eus_mp_web01 : master,false node,false, ingest,true node02 eus_mp_es01 : master,true ...

  8. JMeter压测上对于并发的认识误区

    1.误区 在JMeter压测过程中,我们通常认为1s内100的并发量(即:QPS为100)的设置如下: 此时,没有再添加额外的控制器.上述中的参数设置解释:Number of Threads(user ...

  9. Kubernetes 清除持续 Terminating 状态的Pods

    强制删除所有Pods -- 谨慎使用 kubectl delete pods --all --grace-period=0 --force

  10. 《Effective Java》第2章 对所有对象都通用的方法

    第10条:覆盖equals时,请遵守通用约定 1.使用==来比较两个对象的时候,比较的是两个对象在内存中的地址是否相同(两个引用指向的是否为同一个对象):Object中定义的equals方法也是这样比 ...