SpringBoot核心功能之配置文件(yaml)

SpringBoot核心功能之配置文件

一、文件类型

1、properties

同以前的用法一样。

2、yaml

基本语法:

  1. key: value(value前有空格)
  2. 大小写敏感
  3. 使用缩进表示层级关系(缩进不允许tab,只允许空格)
  4. #表示注释
  5. ''与""表示字符串的内容会被转移/不转义

数据类型

1、字面量:单个的不可再分的值。如date、boolean、string、number、null
表示方法为:

k: v

2、对象:键值对的集合。map、hash、set、object
行内写法:

k: {k1: v1,k2: v2,k3: v3}
  或者:
k:
  k1: v1
  k2: v2
  k3: v3

3、数组:一组按次序排列的值。array、list、queu

k: [v1,v2,v3]
#或者
K: 
  - v1
  - v2
  - v3

示例:
application.yaml文件

person:
  userName: zhangsan
  boss: true
  birth: 2000/1/9
  age: 22
  pet:
    name: hua
    weight: 80
  interests:
    - football
    - computer
  animal:
    - dog
    - cat
  score:
    english: 80
    math: 90
  allPets:
    sick:
      - {name: hue,weight: 60}
      - {name: hub,weight: 50}
    health:
      - {name: huc,weight: 40}

Person.class

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Pet pet;
    private String[] interests;
    private List<String> animal;
    private Map<String,Object> score;
    private Map<String,List<Pet>> allPets;
}

二、配置提示

为项目添加配置处理器依赖,并且在打包时将其移除。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值