目录
编辑⚪加上@ConfigurationProperties注解后出现的问题及解决方法
四、@Value 与@ConfigurationProperties的对比
SpringBoot使用一个全局的配置文件 , 配置文件名称是固定的
修改SpringBoot自动配置的默认值,因为SpringBoot在底层都给我们自动配置好了;
YAML是"YAML Ain't a Markup Language"(YAML不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言),但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名。
YAML 支持以下几种数据类型:

Dog类:
- package com.zz.springboot02config.pojo;
-
- import org.springframework.stereotype.Component;
-
- @Component
- public class Dog {
- private String name;
- private Integer age;
-
- public Dog() {
- }
-
- public Dog(String name, Integer age) {
- this.name = name;
- this.age = age;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- @Override
- public String toString() {
- return "Dog{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
Person类:
- package com.zz.springboot02config.pojo;
-
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
-
- @Component
- public class Person{
- private String name;
- private Integer age;
- private Boolean happy;
- private Date birth;
- private Map
maps; - private List
- private Dog dog;
-
- public Person() {
- }
-
- public Person(String name, Integer age, Boolean happy, Date birth, Map
maps, List { - this.name = name;
- this.age = age;
- this.happy = happy;
- this.birth = birth;
- this.maps = maps;
- this.lists = lists;
- this.dog = dog;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- public Boolean getHappy() {
- return happy;
- }
-
- public void setHappy(Boolean happy) {
- this.happy = happy;
- }
-
- public Date getBirth() {
- return birth;
- }
-
- public void setBirth(Date birth) {
- this.birth = birth;
- }
-
- public Map
getMaps() { - return maps;
- }
-
- public void setMaps(Map
maps) { - this.maps = maps;
- }
-
- public List
- return lists;
- }
-
- public void setLists(List {
- this.lists = lists;
- }
-
- public Dog getDog() {
- return dog;
- }
-
- public void setDog(Dog dog) {
- this.dog = dog;
- }
-
- @Override
- public String toString() {
- return "Person{" +
- "name='" + name + '\'' +
- ", age=" + age +
- ", happy=" + happy +
- ", birth=" + birth +
- ", maps=" + maps +
- ", lists=" + lists +
- ", dog=" + dog +
- '}';
- }
- }

运行测试代码:


测试方法:
- @SpringBootTest
- class Springboot02ConfigApplicationTests {
- @Autowired
- //@Qualifier 指定具体某一个
- private Person person;
-
- @Test
- void contextLoads() {
- System.out.println(person);
- }
- }
在实体类上加上 @ConfigurationProperties注解
-
- /*
- @ConfigurationProperties作用:
- 将配置文件中配置的每一个属性的值,映射到这个组件中;
- 告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定
- 参数 prefix = “person” : 将配置文件中的person下面的所有属性一一对应
- */
- @Component //注册bean
- @ConfigurationProperties(prefix = "person")
- public class Person {
- private String name;
- private Integer age;
- private Boolean happy;
- private Date birth;
- private Map
maps; - private List
- private Dog dog;
- }
运行结果
⚪加上@ConfigurationProperties注解后出现的问题及解决方法
在pom.xml中添加依赖
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-configuration-processorartifactId>
- <optional>trueoptional>
- dependency>
在实体类上添加注解,加载指定的配置文件
- //加载指定的配置文件
- @PropertySource(value = "classpath:application.properties")
- public class Person{
- //SPEL表达式取出配置文件的值
- @Value("${name}")
- private String name;
- private Integer age;
- private Boolean happy;
- private Date birth;
- private Map
maps; - private List
- private Dog dog;

- @SpringBootTest
- class Springboot02ConfigApplicationTests {
- @Autowired
- //@Qualifier 指定具体某一个
- private Person person;
-
- @Test
- void contextLoads() {
- System.out.println(person);
- }
- }
运行结果:

| @ConfigurationProperties | @Value | |
|---|---|---|
| 功能 | 批量注入配置文件中的属性 | 依次指定 |
| 松散绑定(松散语法) | 支持 | 支持 |
| SpEL | 不支持 | 支持 |
| JSR303数据校验 | 支持 | 不支持 |
| 复杂类型封装 | 支持 | 不支持 |
1、@ConfigurationProperties只需要写一次即可 , @Value则需要每个字段都添加
2、松散绑定:这个什么意思呢? 比如我的yml中写的last-name,这个和lastName是一样的, - 后面跟着的字母默认是大写的。这就是松散绑定。
3、JSR303数据校验 , 这个就是我们可以在字段是增加一层过滤器验证 , 可以保证数据的合法性
4、复杂类型封装,yml中可以封装对象 , 使用value就不支持