Back
Featured image of post 03 Spring Boot

03 Spring Boot

The post talk how to set configuration file using yaml syntax, and how to build binding between configuration file and class (component).

Configuration File

  • There are two kind of file format can be used in Spring Boot
    1. application.properties
    2. application.yml (recommended)

Why Need Config File

  • To modify defaule value which be initialized by Spring Boot.

YAML vs. XML vs. properties

  • There are three kind of configuration file type we usually.

  • YAML

    • server:
              prot: 8081
      
  • XML

    • <server>
              <prot>8081</prot>
      </server>
      
  • properties

    • server.prot: 8081
      

YAML Expression Syntax

  • Presents Number, String, Boolean value

    • double quotes " "

      input: name: "daniel \n cool"     \n 捒葌
      
    • output: daniel 捒葌 cool

    • single quotes ' '

      input: name: "daniel \n cool"
      
      • output: daniel \n cool
  • Presents Objece, Map

    • friends:
              lastName: daniel
              age: 21
      
    • friends: {lastName: daniel,age: 21}
      
  • Presents List, Set

    • pets:
          - cat
          - dog
          - pig
      
    • pets: [cat,dog,pig]
      
  • Sample

    • person:
          lastName: hello
          age: 18
          boss: false
          birth: 2017/12/12
          maps: {k1: v1,k2: 12}
          lists:
                  ‐ lisi
                  ‐ zhaoliu
              dog:
                  name: 小狗
                  age: 12
      

Build Relationship Between YAML and Class

  • Sample

    • @Component  
      // make this to container component, so that to use @ConfigurationProperties
      @ConfigurationProperties(prefix="person") 
      //make binding between YAML and this class, prefix is used to specify a target attribute 
      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 dog; // this is a object
      }
      
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy