4.1 yaml配置注入

  1. 给实体类赋值
    1. 方式一:使用@VAlue()+@Component
      1. @Data
        @AllArgsConstructor
        @NoArgsConstructor
        @Component
        public class Dog implements Serializable {
            @Value("二哈")
            private String name;
            @Value("99")
            private Integer age;
        }
    2. 方式二:使用 *.properties配置 + @PropertiesSource(value = "classpath:nbg.properties") + @Value("${name}")
      1. 例如nbg.properties → name = 张无忌
      2. 
        
        @Component
        @PropertySource(value = "classpath:nbg.properties")
        public class Person implements Serializable {
        /**
        * 使用@Vale("${name}") + PropertySource时,需要每个都添加注解
        */
        @Value("${name}")
        private String name;
        private Integer age;
        private Boolean happy;
        private Date birth;
        private Map<String, Object> maps;
        private List<Object> lists;
        private Dog dog;
        }
    3. 方式二:使用applicatio.ymal配置,@Component + @ConfigurationProperties(prefix = "person")
      1. yaml书写格式
        1. 注意空格
        2. 有严格的缩进规则
        3. 属性的值和大小敏感
          • 例:同一个层级
            • cat:
            • person:
          • 例:两个层级
            • cat:
            •     name:空格nbg
            •     age:空格19
          • 例:大小写
            • CAT:
            •     name:空格NBG
        4. 字符串默认不用加双引号和单引号
          1. 双引号:不会转义特殊字符串的内容,会表达特殊字符串的本身意义
            • name: "nbg mmp"   输出 :nbg 换行   mmp
          2. 单引号:会转义特殊字符串的内容,输出字符串
            • name: ‘nbg cnm’   输出 :nbg   cnm
        5. 对象:
          • cat:
          •    name: nbg
          •    age: 999
        6. map:
          • 方式一:maps: {K1: v1,K2: v2} 
          • 方式二:
            • maps:
            •     K1: v1
            •     K2: v2
        7. list set
          • 方式一:
          • habby:
          •     - code
          •     - game    
          • 方式二:
          • habby: {code,game}