SpringBoot总结(四)——@Value和@ConfigurationProperties的区别

@ConfigurationProperties@Value二者的区别:

@ConfigurationProperties @Value
功能 批量注入配置文件的属性 一个一个的指定
松散绑定 支持 不支持
SPEL 不支持 支持
JSR303数据校验 支持 不支持
复杂类型封装 支持 不支持

一、值注入

1、@ConfigurationProperties的使用
首先导入依赖:

  <!--导入配置文件处理器,配置文件进行绑定就会有提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

定义一个School类:

School.java

@Component
@ConfigurationProperties(prefix = "school")
public class School {
     
    private String lastName;
    private String address;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Student student;

    public String getLastName() {
     
        return lastName;
    }

    public void setLastName(String lastName) {
     
        this.lastName = lastName;
    }

    public String getAddress() {
     
        return address;
    }

    public void setAddress(String address) {
     
        this.address = address;
    }

    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 Student getStudent() {
     
        return student;
    }

    public void setStudent(Student student) {
     
        this.student = student;
    }

    @Override
    public String toString() {
     
        return "School{" +
                "lastName='" + lastName + '\'' +
                ", address='" + address + '\'' +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", student=" + student +
                '}';
    }

Student.java

public class Student {
     
    private String name;
    private Integer age;
    private String score;

    public Student() {
     
    }

    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 String getScore() {
     
        return score;
    }

    public void setScore(String score) {
     
        this.score = score;
    }

    @Override
    public String toString() {
     
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score='" + score + '\'' +
                '}';
    }

配置文件:

application.yml

school:
  lastName: xx学校
  address: 江苏
  birth: 2020/01/01
  maps: {
     k1:v1,k2:v2}
  lists:
    - list1
    - list2
  student:
    name: 张三
    age: 17

测试类:

@SpringBootTest
class Springboot02ApplicationTests {
     
    @Autowired
    School  school;
    @Test
   public void contextLoads() {
     
        System.out.println(school);
    }
}

运行结果:
在这里插入图片描述
2、@Value的使用
@Value 也能从配置文件中获取属性的值,需要一个属性一个属性的进行绑定。

School.java

@Component
//@ConfigurationProperties(prefix = "school")
    public class School {
     
        @Value("XXX高级中学")
        private String lastName;

        @Value("${school.address}") 
        private String address;


        private Date birth;
        private Map<String,Object> maps;
        private List<Object> lists;

        private Student student;

        public String getLastName() {
     
            return lastName;
        }

        public void setLastName(String lastName) {
     
            this.lastName = lastName;
        }

        public String getAddress() {
     
            return address;
        }

        public void setAddress(String address) {
     
            this.address = address;
        }

        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 Student getStudent() {
     
            return student;
        }

        public void setStudent(Student student) {
     
            this.student = student;
        }

        @Override
        public String toString() {
     
            return "School{" +
                    "lastName='" + lastName + '\'' +
                    ", address='" + address + '\'' +
                    ", birth=" + birth +
                    ", maps=" + maps +
                    ", lists=" + lists +
                    ", student=" + student +
                    '}';
        }

测试结果:
在这里插入图片描述

二、松散绑定

在JavaBean里写的是lastName(驼峰写法),配置文件里可以写成lastName,也可以写成last-name。
SpringBoot总结(四)——@Value和@ConfigurationProperties的区别_第1张图片
测试结果:
在这里插入图片描述
注意:@ConfigurationProperties 是支持这种命名的;而@Value 是不支持!

在这里插入图片描述

三、SPEL

SpEL即Spring表达式语言(Spring Expression Language)。SpEL表达式的默认格式为: #{expression}。SpEL表达式以“#”开头,表达式主体包围在花括号中。

@Component
public class Person {
     
    private  String name;

    @Value("#{2*6}")
    private  Integer age;

测试结果:
在这里插入图片描述
@ConfigurationProperties 不支持 SPEL不再演示。

四、JSR303校验

注:使用之前需要添加jar包:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

示例代码:

@Component
@Validated
@ConfigurationProperties(prefix = "school")
    public class School {
     

      // @Value("${school.last_name}") 
        @Length(min=5, max=20, message="用户名长度必须在5-20之间")
        private String lastName;

application.yml

school:
  last_name: 学校

运行效果:
SpringBoot总结(四)——@Value和@ConfigurationProperties的区别_第2张图片
注:@ConfigurationProperties支持 JSR303 数据校验。

         @Value("${school.last_name}") 
        @Length(min=5, max=20, message="用户名长度必须在5-20之间")
        private String lastName;

在这里插入图片描述
可以看出:@Value是不支持 JSR303 数据校验的。

五、复杂类型封装

  @Value("${school.student}")
        private Student student;

这样程序运行是直接报错的。@Value不支持复杂类型封装。在上面的例子中已经证明@ConfigurationProperties是支持复杂类型封装。

若文章中有错误的地方欢迎大家反馈或者留言,十分感谢!!!

你可能感兴趣的:(Spring,Boot)