SpringBoot配置文件基本语法和数据类型

配置文件

1、文件类型

1.1、properties

同以前的properties用法

1.2、yaml

1.2.1、简介

YAML 是 “YAML Ain’t Markup Language”(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。

非常适合用来做以数据为中心的配置文件

1.2.2、基本语法
  • key: value;kv之间有空格

  • 大小写敏感

  • 使用缩进表示层级关系

  • 缩进不允许使用tab,只允许空格

  • 缩进的空格数不重要,只要相同层级的元素左对齐即可

  • '#'表示注释

  • 字符串无需加引号,如果要加,''与""表示字符串内容 会被 转义/不转义

1.2.3、数据类型
  • 字面量:单个的、不可再分的值。date、boolean、string、number、null

  • 对象:键值对的集合。map、hash、set、object

  • 数组:一组按次序排列的值。array、list、queue

@ConfigurationProperties(prefix = "person") //配置文件的前缀
@Component  //注入容器
@ToString
@Data
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 Set<Double> salarys;
    private Map<String, List<Pet>> allPets;
}


server:
  port: 8877

person:
  username: "张三 \n 李四"
#  单引号会转义 \n 作为字符串输出,双引号不会转义 \n 作为换行输出
  boos: true
  birth: 2022/03/08
  age: 8
#  interests: [篮球,足球]
  interests:
    - 篮球
    - 足球
  animal: [dog,cat]
#  score: {"语文":100,"数学":50}
  score:
    english: 80
    math: 90
  salarys:
    - 588.25
    - 996.36
  pet:
    name: 阿狗
    weight: 30
  allPets:
    slick:
      - {name: 阿猫,weight: 10}
      - name: 阿虎
        weight: 120
    health:
      - {name: 阿虫,weight: 1}
      - name: 阿羊
        weight: 70

2、配置提示

自定义的类和配置文件绑定一般没有提示。

		<!-- 配置管理器开 -->        
		<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>

2、配置提示

自定义的类和配置文件绑定一般没有提示。

		<!-- 配置管理器开 -->        
		<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、付费专栏及课程。

余额充值