In Spring Boot, the configuration file is application.yml or application.properties.

Yml grammar

The syntax of a YML configuration file is as follows:

  • key:[space] value
  • Case sensitivity
  • Spaces (Tab) control hierarchies

Value can be a simple data type (number, string) or a complex data type (array, object, collection, Map, etc.).

Note: For strings, the default is no double or single quotes.

Configuration file injection

Assume that the configuration file contains the following contents:

coder:
  name: smartpig  // Simple data
  lang: [Java,Kotlin] //List
  softwares: {cut: pr,image: ps} //Map
  pet:  / / object
    name: bigdog
    type: dog
Copy the code
  • Use @value to get the Value in the configuration file

In the controller, use @value (” ${} “) to get the Value in the configuration file, as follows to get the Value of name in the coder

@Controller
public class Hello {
    @Value("${coder.name}")
    private String name;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(a){
        return "hello "+ name; }}Copy the code
  • The configuration file is mapped using @ConfigurationProperties(perfix=””) and @Compent

    Write Pet class, write Coder class

@Component
@ConfigurationProperties(prefix = "coder")
public class Coder {
    private String name;
    private List<String> lang; / / array
    private Map<String,String> softwares; //Map
    private Pet pet; / / object
    / / omit getter/setter/toString
}
Copy the code

The test is then performed in the test class

@SpringBootTest
class DemoSpringbootApplicationTests {
    @Autowired
    Coder coder;
    
    @Test
    void contextLoads(a) { System.out.println(coder.toString()); }}Copy the code

Enter the value in the configuration file

Using a plug-in makes writing the corresponding configuration in YML prompt

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
Copy the code

Do the mapping in the Properties configuration file

If you use a properties configuration file, everything is the same as in YML except for the writing format.

coder.name=smartpig
coder.lang=Java,Kotlin
coder.softwares.cut=pr
coder.softwares.image=ps
coder.pet.name=bigdog
coder.pet.type=dog
Copy the code

@value and @ConfigurationProperties

The serial number @Value @ConfigurationProperties
1 A single injection Batch injection
2 Support for EL expressions EL expressions are not supported
3 Check not supported Support check
4 Automatic conversion to hump naming is not supported Automatic conversion to hump naming is supported
5 Complex type injection is not supported Support for complex type injection
  • EL expression

    Modify the configuration file as follows

    coder:
      name: ${smartpig} // Change to the EL expression
      lang: [Java,Kotlin]
      softwares: {cut: pr,image: ps}
      pet:
        name: bigdog
        type: dog
    Copy the code

    ConfigurationProperties: Could not resolve ‘smartPig’ in value “${smartPig}”; If you use @value, you don’t get an error

  • check

    Added a check to the Coder class to verify that coder.name is not an empty string

    @Component
    @Validated
    @ConfigurationProperties(prefix = "coder")
    public class Coder {
        @NotEmpty
        private String name;
    }
    Copy the code

    Change the configuration file to

    coder:
      name:  / / to empty
      lang: [Java,Kotlin]
      softwares: {cut: pr,image: ps}
      pet:
        name: bigdog
        type: dog
    Copy the code

    Error: @configurationProperties

    Property: coder. Name Value: Origin: class Path Resource [Application. Yml]:2:8 Reason: Cannot be emptyCopy the code

    Using @value does not validate

  • The complex type

    Using @value to inject coder. Name is fine, if using @value to inject complex types such as List

    @Component
    //@ConfigurationProperties(prefix = "coder")
    public class Coder {
        private String name;
        @Value("${coder.lang}")  // Use complex type injection lang
        private List<String> lang;
        private Map<String,String> softwares;
        private Pet pet;
    }
    Copy the code

    Could not resolve ‘coder. Lang ‘in value “${coder. Lang}”

conclusion

Today I’ve shared how configuration file data can be injected into variables or classes; You can use @Value for small amounts of data, but you can use @ConfigurationProperties for complex data.

Thanks for watching.