SpringBoot系列之@Value和@ConfigurationProperties
继上一篇博客SpringBoot系列之YAML配置用法之后,再写一篇@Value、@ConfigurationProperties的对比博客
这两个主键都是可以获取配置文件属性的,不过是有比较大的区别的,所以本博客做一下对比,ok,继续拿上一篇博客的例子来实验
## 测试ConfigurationProperties
user:
userName: root
isAdmin: true
regTime: 2019/11/01
isOnline: 1
maps: {k1 : v1,k2: v2}
lists:
- list1
- list2
address:
tel: 15899988899
name: 上海市
- 松散绑定(Relaxed binding)
比如例子user: userName也可以表示为user: user-name(大写用-符号),user: username表示为user: user_name(小写用_符号),比如支持这种写法的就是支持松散绑定
例子:
将yml的配置改一下
user:
user-name: root
先用@ConfigurationProperties测试
package org.muses.jeeplatform.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "user")
public class User {
private String userName;
private boolean isAdmin;
private Date regTime;
private Long isOnline;
private Map<String,Object> maps;
private List<Object> lists;
private Address address;
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", isAdmin=" + isAdmin +
", regTime=" + regTime +
", isOnline=" + isOnline +
", maps=" + maps +
", lists=" + lists +
", address=" + address +
'}';
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public boolean isAdmin() {
return isAdmin;
}
public void setAdmin(boolean admin) {
isAdmin = admin;
}
public Date getRegTime() {
return regTime;
}
public void setRegTime(Date regTime) {
this.regTime = regTime;
}
public Long getIsOnline() {
return isOnline;
}
public void setIsOnline(Long isOnline) {
this.isOnline = isOnline;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Junit测试,可以读到数据
User{userName='root', isAdmin=false, regTime=Fri Nov 01 00:00:00 CST 2019, isOnline=1, maps={k1=v1, k2=v2}, lists=[list1, list2], address=Address{tel='15899988899', name='上海市'}}
改一下,用@Value去读
user:
userName: root
is-admin: true
@Value("${userName}")
private String userName;
@Value("${is-Admin}")
private boolean isAdmin;
ok,发现报错了
org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'boolean'
ok,本博客例子是用application.yml配置的,对于application.properties的本博客没介绍,不过还是要随便提一下,application.properties文件的默认文件编码是utf8,所以写中文时候有时候会出现乱码问题
这时候可以修改编码:file->settings->Editor->file encodings

- SpEL表达式
ok,再对比一下这两种注解对SpEL表达式的支持,对于SpEL表达式的可以参考:https://blog.csdn.net/fanxiaobin577328725/article/details/68942967
本博客之对比一下,这两种注解
ok,先改下配置,看看@ConfigurationProperties能获取到?
user:
isOnline: #{1*1}
debug了一下,发现不能正常计算
ok,验证@value
@Value("#{1*1}")
private Long isOnline;
junit测试,ok,@Value是支持的
User{userName='null', isAdmin=false, regTime=null, isOnline=1, maps=null, lists=null, address=null}
- JSR303数据校验
同样对于JSR303本博客也不进行详细介绍,详情可以参考博客:https://www.ibm.com/developerworks/cn/java/j-lo-jsr303/index.html
@ConfigurationProperties验证:
@AssertTrue
private boolean isAdmin;
junit测试,发现可以支持
@Value验证
@AssertTrue
@Value("${isAdmin}")
private boolean isAdmin;
验证,校验发现不起效
- 复杂类型封装
ok,验证@Value是否支持对象类型和list类型,在上篇博客,很显然验证了@ConfigurationProperties是支持对象类型和list类型获取的
所以,本博客验证一下@Value是否支持就可以
@Value("${maps}")
private Map<String,Object> maps;
junit测试,发现类型转换错误
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Map';
综上,可以归纳一下@Value和@ConfigurationProperties两种属性的区别
| @ConfigurationProperties | @Value | |
|---|---|---|
| 功能对比 | 批量注入配置文件属性 | 一个一个属性的注入 |
| 松散绑定 | 支持 | 不支持 |
| SpEL | 不支持 | 支持 |
| JSR303数据校验 | 支持 | 不支持 |
| 复杂类型封装 | 支持 | 不支持 |
所以,@ConfigurationProperties适用与注入配置文件整个对应bean的全部属性,而@Value正如其名称一样,适合注入配置文件单个值
SpringBoot系列之@Value和@ConfigurationProperties的更多相关文章
- SpringBoot系列之@PropertySource用法简介
SpringBoot系列之@PropertySource用法简介 继上篇博客:SpringBoot系列之@Value和@ConfigurationProperties用法对比之后,本博客继续介绍一下@ ...
- SpringBoot系列之学习教程汇总
对应SpringBoot系列博客专栏,例子代码,本博客不定时更新 一.配置篇 SpringBoot系列之@PropertySource读取yaml文件 >> source down ...
- SpringBoot系列之从入门到精通系列教程
对应SpringBoot系列博客专栏,例子代码,本博客不定时更新 Spring框架:作为JavaEE框架领域的一款重要的开源框架,在企业应用开发中有着很重要的作用,同时Spring框架及其子框架很多, ...
- Springboot 系列(九)使用 Spring JDBC 和 Druid 数据源监控
前言 作为一名 Java 开发者,相信对 JDBC(Java Data Base Connectivity)是不会陌生的,JDBC作为 Java 基础内容,它提供了一种基准,据此可以构建更高级的工具和 ...
- Springboot 系列(十五)如何编写自己的 Springboot starter
1. 前言 Springboot 中的自动配置确实方便,减少了我们开发上的复杂性,那么自动配置原理是什么呢?之前我也写过了一篇文章进行了分析. Springboot 系列(三)Spring Boot ...
- SpringBoot系列之集成Thymeleaf用法手册
目录 1.模板引擎 2.Thymeleaf简介 2.1).Thymeleaf定义 2.2).适用模板 3.重要知识点 3.1).th:text和th:utext 3.2).标准表达式 3.3).Thy ...
- SpringBoot系列之集成Druid配置数据源监控
SpringBoot系列之集成Druid配置数据源监控 继上一篇博客SpringBoot系列之JDBC数据访问之后,本博客再介绍数据库连接池框架Druid的使用 实验环境准备: Maven Intel ...
- SpringBoot系列之@PropertySource读取yaml文件
SpringBoot系列之@PropertySource支持yaml文件读取 最近在做实验,想通过@PropertySource注解读取配置文件的属性,进行映射,习惯上用properties都是测试没 ...
- SpringBoot系列之配置文件占位符使用
SpringBoot系列之配置文件占位符使用 Springboot占位符支持的有随机数和配置的值等等,本博客主要介绍的是随机数和获取属性配置值的简单用法 随机数获取 支持的写法有: ${random. ...
随机推荐
- Maven报错: Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp
郁闷了两天,创建maven项目时,eclipse报错:Could not resolve artifact org.apache.maven.archetypes:maven-archetype-we ...
- 问题:程序编译通过,但是执行时报错:coredump
问题描述: 在一个客户现场搭建环境时,遇到了一个棘手的问题,C代码编译通过后,无法正常运行,启动会出现“coredump”错误. 运行环境为新搭建的AIX6.1,数据库为Oracle11.2.0.2. ...
- .Net Core 商城微服务项目系列(十三):搭建Log4net+ELK+Kafka日志框架
之前是使用NLog直接将日志发送到了ELK,本篇将会使用Docker搭建ELK和kafka,同时替换NLog为Log4net. 一.搭建kafka 1.拉取镜像 //下载zookeeper docke ...
- Kubernetes 系列(二):Deployment 扩容
(1)首先我们创建一个nginx的Deployment,采用官方的yaml: kubectl create -f https://kubernetes.io/docs/user-guide/nginx ...
- Docker5-docker私库的搭建及常用方法-harbor-registry方式
一.简介 1.官方已经提供registry镜像为什么还需要用harbor 1)registry缺少镜像清理机制,可以push但是不能删除,耗费空间 2)registry缺乏相应的扩展机制 3)harb ...
- 04-Django模型(1)
---恢复内容开始--- 模型 MTV图解 ORM ORM全拼:Object-Relation-Mapping翻译就是对象关系映射.在MVC/MTV设计模式中的Model模块中都包括ORM.主要实现模 ...
- 从零开始的vue学习笔记(四)
组件注册 组件名 Vue.component('my-component-name', { /* ... */ }) 这里的my-component-name就是组件名,组件名的取法可以参考指南 ke ...
- Android Studio:多包名打包
来自:http://m.blog.csdn.net/u011315960/article/details/73251196 前言 最近有点小忙,博客都落下了,今天赶紧写点东西补上. 前几天商务找我,想 ...
- 窥见云技术未来大势,腾讯云Techo开发者大会即将在京召开
云.物联网.5G.人工智能……一项项技术的突破带来了天翻地覆的变化,开发者们是如何一次次地进行天马行空的创意和极限突破?2019年11月6日-7日,由腾讯云主办的首届Techo开发者大会将在北京嘉里大 ...
- Qt5教程: (5) Lambda匿名函数的使用
Lambda是C++11的新特性, 首先看看你的.pro项目文件里有没有CONFIG += c++11这句话, 没有就加上. 下面新建一个工程, 具体步骤就不多说了 然后给主窗口添加一个按钮b, 并且 ...