[学习笔记] SpringBoot 的配置文件、yaml语法、配置注入、松散绑定

配置文件

SpringBoot 有两种配置文件格式,二选一即可,官方推荐 yaml:

  • application.properties key=value的格式
  • application.yaml key: value的格式

配置文件位置

SpringBoot会从这四个位置全部加载主配置文件;互补配置。优先级从高到低。

  • --spring.config.location=F:/application.yaml
  • /config/application.yaml
  • /application.yaml
  • /src/main/resources/config/application.yaml
  • /src/main/resources/application.yaml

yaml 格式特点

  • 冒号后面必须有一个空格,不能省略
  • 以缩进来控制层级关系,左边对齐的为同一层级
  • 属性和值的大小写敏感
  • 双引号中的特殊字符会转义输出,如"hello \n world"会发生换行
  • 双引号中的特殊字符会原样输出,如‘hello \n world’所见即所得

配置示例

# src/main/resources/application.properties
server.port=8081
# src/main/resources/application.yaml
server:
    port: 8081

# 对象形式
student:
    name: zhangsan
    age: 18

# 行内对象形式
student: {name: zhangsan,age: 18}

# 数组形式
pets:
    - cat
    - dog
    - pig

# 行内数组形式
pets: [cat,dog,pig]

配置 banner

# src/main/resources/banner.txt
# https://www.bootschool.net/ascii-art
        _ _                                     
      _(9(9)__        __/^\/^\__                
     /o o   \/_     __\_\_/\_/_/_               
     \___,   \/_   _\.'       './_      _/\_    
      `---`\  \/_ _\/           \/_   _|.'_/    
            \  \/_\/      /      \/_  |/ /      
             \  `-'      |        ';_:' /       
             /|          \      \     .'        
            /_/   |,___.-`',    /`'---`         
             /___/`       /____/                

注入 yaml 配置文件(方式一)

package com.wu.helloworld.pojo;

@Component
public class Dog {
    @Value("阿黄")
    private String name;
    @Value("18")
    private Integer age;
}
@SpringBootTest
class HelloworldApplicationTests {
    @Autowired
    Dog dog;

    @Test
    public void contextLoads() {
        System.out.println(dog)
    }
}

注入 yaml 配置文件(方式二)

<!-- 导入配置文件处理器依赖,需要重启IDE -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <optional>true</optional>
</dependency>
package com.wu.helloworld.pojo;

@Component
public class Dog {
    private String name;
    private Integer age;
    
    //构造函数、get、set、toString 等方法  
}
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
    
    //构造函数、get、set、toString 等方法  
}
person:
  name: wu_${random.uuid}
  age: ${random.int}
  happy: false
  birth: 2000/01/01
  maps: {k1: v1,k2: v2}
  lists:
   - code
   - girl
   - music
  dog:
    name: ${person.dogName:默认名字}_旺财
    age: 1
@SpringBootTest
class DemoApplicationTests {
    @Autowired
    Person person;

    @Test
    public void contextLoads() {
        System.out.println(person);
    }
}

注入 properties 配置文件

设置 properties 的编码格式为UTF-8:

File / Settings / File Encodings / Properties Files / UTF-8 && √ Transparent native-to-ascii conversion

# src/main/resources/person.properties
person.name=wu
person.age=18
person.sex=男
@PropertySource(value = "classpath:person.properties")
@Component
public class Person {
    @Value("${person.name}") // 从配置文件中取值
    private String name;
    @Value("#{9*2}")  // #{SPEL} Spring表达式
    private int age;
    @Value("男")  // 字面量
    private String sex;
}

松散绑定

dog:
    first-name: 旺财
    age: 3

@Component
public class Dog {
    private String firstName;  // 可以绑定横杠的配置值
    private Integer age;
}

猜你喜欢

转载自www.cnblogs.com/danhuang/p/12533680.html