SpringBoot核心技术(配置文件)

在这里插入图片描述

配置文件

1、yaml

①简介

YAML 是 “YAML Ain’t Markup Language”(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。
非常适合用来做以数据为中心的配置文件

②基本语法

  • key: value;kv之间有空格
  • 大小写敏感
  • 使用缩进表示层级关系
  • 缩进不允许使用tab,只允许空格
  • 缩进的空格数不重要,只要相同层级的元素左对齐即可
  • '#'表示注释
  • 字符串无需加引号,如果要加,’'与""表示字符串内容 会被 转义/不转义

③数据类型

字面量:单个的、不可再分的值。date、boolean、string、number、null

k: v

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

行内写法:  k: {
    
    k1:v1,k2:v2,k3:v3}
#或
k: 
  k1: v1
  k2: v2
  k3: v3

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

行内写法:  k: [v1,v2,v3]
#或者
k:
 - v1
 - v2
 - v3

实例:

Person 类:

@ConfigurationProperties(prefix = "person")
@Component
@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;
}

Pet 类:

@Data
public class Pet {
    
    
    private String name;
    private Double weight;
}

application.yaml配置文件

person:
  #单引号会将 \n作为字符串输出 双引号会将\n作为换行输出(单引号不转义 双引号转义)
  username: frank #字符串也可以用单引号或者双引号
  boss: true
  birth: 1999/10/30
  age: 21
  #interests:[篮球,足球] (String)
  interests:
    -篮球
    -足球
    -18
  #animal (List)
  animal: [阿猫,阿狗]
  #score (Map)
  score: {
    
    english: 80,math: 90}
  #salary (Set)
  salarys:
    - 9999.98
    - 9999.99
  #pet (Pet类)
  pet:
    name: 阿狗
    weight: 100
  allPets:
    sick:
      - {
    
    name: 阿狗,weight: 99.99}
      - {
    
    name: 阿猫,weight: 99.99}
      - name: 阿虫
        weight: 77.77
    health:
      - {
    
    name: 阿花,weight: 199.99}
      - {
    
    name: 阿明,weight: 199.99}

#也可以通过yaml配置spring中的一些参数
#spring:
#  banner:
#    location:
#    image:
#      bitdepth: 4
#
#  cache:
#    type: redis
#    redis:
#      time-to-live: 1000

关于字符串单双引号转义问题
单引号会将 \n作为字符串输出 双引号会将\n作为换行输出(单引号不转义 双引号转义)

controller组件

@RestController
public class HelloController {
    
    
    @Autowired
    Person person;

    @RequestMapping("/person")
    public Person person(){
    
    
        return person;
    }
}

主程序类

@SpringBootApplication
public class Boot01Helloworld02Application {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(Boot01Helloworld02Application.class, args);
    }
}

执行结果;
在这里插入图片描述

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>

おすすめ

転載: blog.csdn.net/weixin_44742328/article/details/117918010