标签:public ext match htm proc ima efi 用法 nic
继上一篇博客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: 上海市
例子:
将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
本博客之对比一下,这两种注解
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}
@ConfigurationProperties验证:
@AssertTrue
private boolean isAdmin;
junit测试,发现可以支持
@Value验证
@AssertTrue
@Value("${isAdmin}")
private boolean isAdmin;
验证,校验发现不起效
所以,本博客验证一下@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
标签:public ext match htm proc ima efi 用法 nic
原文地址:https://www.cnblogs.com/mzq123/p/11789730.html