The difference and usage of @ConfigurationProperties and @Value in spring boot

Table of contents

Introduction to @Value("")

@configurationProperties

@ConfigurationProperties compared to @Value

@Value("") introduces:

@Value ("") introduction: used to specify the value of a property in the bean, its value can be a literal value, a value obtained from a configuration file, a spel expression

For example:

@Value("${person.last-name}")
private String lastName;
@Value("#{11*2}")
private Integer age;
@Value("true")
private Boolean boss;

The configuration file is configured as follows:

person.last-name=张三

@configurationProperties:

@configurationProperties: read the values ​​of all properties from the configuration file;

For example:

package com.example.springboot02config.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * 将配置文件中配置的每一个属性的值映射到这个组件中
 *@ConfigurationProperties:这个注解的作用是告诉spring boot本类中的所有属性和配置文件中的配置进行绑定;
 * prefix = "person";指定与哪一个下面的属性进行映射
 * 只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能
 * @Component:把这个bean加入到容器中
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dogs;
    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 getDogs() {
        return dogs;
    }
    public void setDogs(Dog dogs) {
        this.dogs = dogs;
    }
    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Person{");
        sb.append("lastName='").append(lastName).append('\'');
        sb.append(", age=").append(age);
        sb.append(", boss=").append(boss);
        sb.append(", birth=").append(birth);
        sb.append(", maps=").append(maps);
        sb.append(", lists=").append(lists);
        sb.append(", dogs=").append(dogs);
        sb.append('}');
        return sb.toString();
    }
}

The configuration file code is as follows:

1.properties file as a configuration file

#idea使用的是utf-8
#配置person的值
person.age=18
person.birth=2017/10/10
person.boss=false
person.maps.k1=v1
person.maps.k2=jiangsu
person.last-name=张三
person.lists=1,a,b,c
person.dogs.name=我是狗一号
person.dogs.age=18

2.yml file as a configuration file

person:
  lastName: zhangsan
  age: 20
  boss: false
  birth: 2017/12/12
  maps: {k1: v1,k2: v2}
  lists:
    - lishi
    - zhaowu
  dogs:
    name: 小狗一号
    age: 8

@ConfigurationProperties compared to @Value:

Similarities and differences between @ConfigurationProperties and @Value:

The same point: first of all, they can be used to automatically configure the value of one or some attributes

Compare:

1. From a functional comparison, @ConfigurationProperties injects properties in the configuration file in batches; while @Value needs to configure the values ​​​​of the specified properties one by one

2. @ConfigurationProperties supports loose binding, @Value does not support loose binding

For example: configure @ConfigurationProperties for the following properties

private String lastName;

The configuration file is as follows (properties file):

person.LAST_NAME=张三
person.last-name=张三
person.last_name=张三

All of the above three named configurators can assign values ​​to attributes;

When using @Value for annotation, you can only use the same name as the configuration file for annotation, for example:

@Value("${person.last_name}")
private String lastName;

configuration file:

person.last_name=张三

3. @ConfigurationProperties does not support spel syntax, @Value supports spel syntax

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

4. @ConfigurationProperties supports JSR303 data verification, @Value does not support data verification

For example, use the following @Value to assign values ​​to attributes, and the validation will not take effect:

@Value("${person.last_name}")
@Email
private String lastName;

Use @ConfigurationProperties to assign values ​​to properties, and the validation will take effect:

package com.example.springboot02config.bean;
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 javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * 将配置文件中配置的每一个属性的值映射到这个组件中
 *@ConfigurationProperties:这个注解的作用是告诉spring boot本类中的所有属性和配置文件中的配置进行绑定;
 * prefix = "person";指定与哪一个下面的属性进行映射
 * 只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能
 * @Component:把这个bean加入到容器中
 */
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {


    @Email
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dogs;
    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 getDogs() {
        return dogs;
    }

    public void setDogs(Dog dogs) {
        this.dogs = dogs;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Person{");
        sb.append("lastName='").append(lastName).append('\'');
        sb.append(", age=").append(age);
        sb.append(", boss=").append(boss);
        sb.append(", birth=").append(birth);
        sb.append(", maps=").append(maps);
        sb.append(", lists=").append(lists);
        sb.append(", dogs=").append(dogs);
        sb.append('}');
        return sb.toString();
    }
}

configuration file:

person.age=18
person.birth=2017/10/10
person.boss=false
person.maps.k1=v1
person.maps.k2=jiangsu
person.last_name=张三
person.lists=1,a,b,c
person.dogs.name=我是狗一号
person.dogs.age=18

Result: The value of the variable lastName does not conform to the mailbox format and execution will throw an exception

Property: person.lastName
Value: "张三"
Origin: class path resource [application.properties] - 9:18
Reason: 不是一个合法的电子邮件地址

5. @ConfigurationProperties supports complex type encapsulation, @Value does not support complex type encapsulation, for example, Map type cannot be configured using @Value

Guess you like

Origin blog.csdn.net/psjasf1314/article/details/128478738