注解@ConfigurationProperties
该注解的作用是将配置文件中配置的每一个属性的值,映射到这个组件中。@ConfigurationProperties :告诉springboot将本类中的所有属性和配置文件中相关的配置进行绑定 prefix = [person]:配置文件中哪个下面的所有属性进行一一映射。简言之,也就是只有这个组件是容器中的组件;才能在容器中提供的@ConfigurationProperties功能。
注解@Value
该注解就是将配置文件中的某项值读出来,@Value("$(key)"),其中key的值从环境变量、配置文件中获取值
区别
该表格展示了这两个注解的区别,其中,松散语法绑定的定义如下:
松散语法绑定:
- person.firstName : 使用标准方法 - person.first-name : 大写用- - person.first_name : 大写用_ - PERSON_FIRST_NAME : 推荐系统属性使用这种写法
SpEl语法表示:
其中,@Value可以直接计算表达式的值,如:@Value(#{11*2})
JSR303数据校验:
注解@ConfigurationProperties(prefix = "person")可以搭配@Validated使用
复杂类型封装:
支持读入类中的所有属性,比如,想读如person中的所有属性,使用@ConfigurationProperties(prefix = "person")
1 2 3 4 5 6 7 8 9 10 |
# 配置person的值 person.last-name=zhangsan person.age=18 person.birth=2017/12/12 person.boss=false person.maps.k1=v1 person.maps.k2=14 person.lists=a,b,v person.dog.name=二哈 person.dog.age=2 |
使用方法区别:
配置文件无论是yml还是properties他们都能获取
如果说,我们只是说在某个业务逻辑中需要获取一下配置文件中的某项值,就使用@Value
如果说,我们专门编写了一个javaBean来和配置文件映射,我们就直接使用
配置文件注入值数据校验
注入值校验主要用的是@Validated注解,像代码中private String lastName;可以使用@Email,虽然使用姓名使用邮件格式可能不太合适,此处只是为了举例子,意思是lastName必须是邮箱格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
@Component @ConfigurationProperties (prefix = "person" ) @Validated public class Person { /** * <bean class="Person"> * <property name="lastNme" value="字面量/${key}从环境变量、配置文件中获取值/#{SpEl}"> * * </property>> * </bean> */ //lastName必须是邮箱格式 // @Value("${person.last-name}") private String lastName; // @Value("#{11*2}") private Integer age; // @Value("true") private Boolean boss; private Date birth;
// @Value("${person.maps}") private Map<String,Object> maps; private List<Object> lists; private 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; }
@Override public String toString() { return "Person{" + "lastName='" + lastName + '\ '' + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps=" + maps + ", lists=" + lists + ", dog=" + dog + '}' ; } } |
到此这篇关于SpringBoot中注解@ConfigurationProperties与@Value的区别与使用详解的文章就介绍到这了,更多相关SpringBoot @ConfigurationProperties @Value内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/qq_38530648/article/details/120238361
查看更多关于SpringBoot中注解@ConfigurationProperties与@Value的区别与使用的详细内容...