4-3 SpringBoot 配置文件映射JavaBean — @Value获取值和@ConfigurationProperties获取值比较
用这个@Value注解可以不导入之前那个GAV
我们之前配置文件 绑定映射 到JavaBean的值都是用 @ConfigurationPropertie
用@Value也是可以的,它是Spring的一个注解,无论配置文件是 yml还是properties他们都能获取到值; 而且支持以下SpEL写法。
2021年10月2日 13:28:30 补充: @value 这个注解 后面加上 : 默认值 如果找不到的话 就用默认值。。。
配置文件(properties文件):

#private String lastName; #private Integer age; #private Boolean boss; #private Date birth; #private Map<String,Object> maps; #private List<Object> lists; #private Dog dog; #============================================================= #person.lastName 相等于 person.last-name【松散写法】 person.last-name=BiHu person.age=18 person.boss=false person.birth=2025/10/10 person.maps.k1=value1 person.maps.k2=value2 person.lists=v1,v2,v3,v4 person.dog.name=小勾勾 person.dog.age=2
JavaBean.java:

package com.bihu.Bean; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; @Component //@ConfigurationProperties(prefix = "person") public class JavaBean { /** * 支持下面三种写法 * value="字面量/${key}从环境变量、配置文件中获取值/#{SpEL}" 下面一一演示: * */ @Value("${person.last-name}") //直接字面量 private String lastName; @Value("#{2*3}") private Integer age; @Value("false") private Boolean boss; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; @Override public String toString() { return "Person{" + "lastName='" + lastName + '\'' + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps=" + maps + ", lists=" + lists + ", dog=" + dog + '}'; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getBoss() { return boss; } public void setBoss(Boolean boss) { this.boss = boss; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } 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 Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } }
里面三种写法已经展示,运行测试:

package com.bihu; import com.bihu.Bean.JavaBean; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) //有了@RunWith(SpringRunner.class)这些类才能实例化到spring容器中,自动注入才能生效, @SpringBootTest public class ApplicationTests { //这里测试JavaBean @Autowired JavaBean Person; @Test public void contextLoads() { System.out.println(Person); } }
结果:
Person{lastName='BiHu', age=6, boss=false, birth=null, maps=null, lists=null, dog=null}
可以看出 ,lastName是直接获取到配置文件中的数据写入,然后 age 是用 #{表达式} 直接算出注入,boss 为false 是直接字面量写入的。
@Value获取值和@ConfigurationProperties获取值比较⭐⭐⭐
@ConfigurationProperties | @Value | |
功能 | 批量注入配置文件中的属性 | 一个个指定 |
松散绑定(松散语法) | 支持 | 不支持 |
SpEL | 不支持 | 支持 |
JSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |
1. 功能不用多说,都知道
2.松散语法: 就是驼峰用-表示,即: lastName 相当于 last-Name , 如果配置文件是“ person.last-name=BiHu ”,如果用@Value这样写: @Value("${person.lastName}") ,会获取不了会报错,因为@Value不支持松散语法,反之如果是 @ConfigurationProperties注解来注入,就不会,因为他支持这个松散写法。
关于是否支持松散语法提示: 不用想那么多,配置文件怎么样写那就怎么样写即可...
3.spel表达式 ,其实就是 那个 “#{表达式}” 这个表达式就是 spel表达式。
4.JSR303数据校验:
JavaBean的值如果要校验,我们在JavaBean放一个@Validated 说明映射值绑定需要校验,然后我们在 某成员变量写一个注解 @Email 表示下面这个成员变量需要是邮箱格式,会校验的,如果不是则报错!
JavaBean.java:

package com.bihu.Bean; import org.hibernate.validator.constraints.Email; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import org.springframework.validation.annotation.Validated; import java.util.Date; import java.util.List; import java.util.Map; @Component @ConfigurationProperties(prefix = "person") @Validated //这个注解说明JavaBean的值映射需要校验 public class JavaBean { @Email //这个注解说明 lastName一定得邮箱数据类型 private String lastName; private Integer age; private Boolean boss; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; @Override public String toString() { return "Person{" + "lastName='" + lastName + '\'' + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps=" + maps + ", lists=" + lists + ", dog=" + dog + '}'; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getBoss() { return boss; } public void setBoss(Boolean boss) { this.boss = boss; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } 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 Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } }
配置文件(properties)

#private String lastName; #private Integer age; #private Boolean boss; #private Date birth; #private Map<String,Object> maps; #private List<Object> lists; #private Dog dog; #============================================================= #person.lastName 相等于 person.last-name【松散写法】 #person.last-name=bihu 报错!!!!! # person.last-name=123@qq.com 不报错!!!!因为是一个邮箱格式的写法 person.age=18 person.boss=false person.birth=2025/10/10 person.maps.k1=value1 person.maps.k2=value2 person.lists=v1,v2,v3,v4 person.dog.name=小勾勾 person.dog.age=2
这就是JSR303数据校验。
5.复杂类型封装:
其实就是List、Map、数组和对象,@Value注解是不支持注入的,@ConfigurationProperties是支持的,例如下面注入maps的时候会报错:
@Value("${person.maps}") //这样会报错!因为@Value不支持特殊数据类型 只支持基本数据类型 private Map<String,Object> maps;
总结:
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;【记住:单独需要某项值】
我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;
一般都是用@ConfigurationProperties!
这就是@Value的使用 和 @Value获取值和@ConfigurationProperties获取值 不同
千万别弄混:
@Value 是有了配置文件 然后在配置文件中取某个数据注入【不需要依赖GAV】
@ConfigurationProperties 是直接在配置文件中的数据绑定过来。【需要依赖GAV】
本文来自博客园,作者:咸瑜,转载请注明原文链接:https://www.cnblogs.com/bi-hu/p/15073855.html