This is the seventh day of my participation in the First Challenge 2022. For details: First Challenge 2022.

In the last article we looked at using @ConfigurationProperties to get the Value of a configuration file. This time we’ll look at another method, @Value

The Value method gets the Value of the configuration file

We comment out @confugurationProperties and add a value comment ($matches #)

/** * maps the value of each attribute configured in the configuration file to the component *@configurationproperties: tells SpringBoot to bind all properties in this class to the relevant configuration in the configuration file * prefix="person" Map all properties under person in the configuration file * Only the component is provided by the container if it is a component in the container@ConfigurationPropertiesThe function of * /
@Component
//@ConfigurationProperties(prefix="person")
public class Person {

    /* 
       
        
        */
      
    @Value("${person.last-name}")
    private String lastName;
    @Value("#{10*2}")
    private Integer age;
    @Value("true")
    private Boolean boss;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object>lists;
    private Dog dog;
Copy the code

So let’s test that out

In this case, all three of the options we matched were successfully exported

Let’s look at the difference between @ConfigurationProperties and @Value

ConfigurationProperties can be mapped to all of them, while @Values must be mapped one by one

${person.last-name}”; ${person.last-name}”; Experiment with @ConfigurationProperties and see that it works. So, @ConfigurationProperties supports loose binding, @Value does not

SpEL: We support arithmetic when we use @Value. When we use @ConfigurationProperties, we get an error when we try to change the age of the configuration file to arithmetic

JSR303 Data verification: We add a @validated annotation and a validation rule, such as @emile, to verify whether the match is in email format. So let’s check at sign ConfigurationProperties

@Component
@ConfigurationProperties(prefix="person")
@Validated
public class Person {

    /* 
       
        
        */
      
    //@Value("${person.last-name}")
    // Last-name must be in the mailbox format
    @Email
    private String lastName;
    //@Value("#{10*2}")
    private Integer age;
    //@Value("true")
    private Boolean boss;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object>lists;
    private Dog dog;
Copy the code

We found a validation error indicating that @ConfigurationProperties supports JSR303 data validation and then we checked with @Value

@Component
//@ConfigurationProperties(prefix="person")
@Validated
public class Person {

    /* 
       
        
        */
      

    // Last-name must be in the mailbox format
    @Email
    @Value("${person.last-name}")
    private String lastName;
    @Value("#{10*2}")
    private Integer age;
    @Value("true")
    private Boolean boss;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object>lists;
    private Dog dog;
Copy the code

We found no change, so @Value does not support JSR303 data validation

Complex type encapsulation: Let’s use @value alone to get maps

@Component
//@ConfigurationProperties(prefix="person")
//@Validated
public class Person {

    /* 
       
        
        */
      

    // Last-name must be in the mailbox format
    @Email
    //@Value("${person.last-name}")
    private String lastName;
    //@Value("#{10*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;
Copy the code

We found an error, so @Value does not support complex type encapsulation, which is the biggest difference between @ConfigurationProperties and @Value

Configuration files, either YML or Properties, can get values

Usage:

  1. If we need to get the Value of a configuration file in some business logic, use @value;

So here we can write a Controller

@RestController
public class Controller {

    @Value("${person.last-name}")
    private String name;

    @RequestMapping("/sayHello")
    public String sayHello(a){

        return "Hello "+name; }}@RestController
public class Controller {

    @Value("${person.last-name}")
    private String name;

    @RequestMapping("/sayHello")
    public String sayHello(a){

        return "Hello "+name; }}Copy the code

Run tests, web tests

  1. If we wrote a JavaBean specifically to map to the configuration file, we would just use @ConfigurationProperties