SpringBoot yaml配置文件

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringBoot yaml配置文件

鸣鼓ming   2022-09-08 我要评论

1.基本语法

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

2.数据类型

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、queue

#行内写法:  

k: [v1,v2,v3]

#或者

k:
 - v1
 - v2
 - v3

3.代码测试

User

package com.limi.springboottest2.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String userName;
    private Integer age;
    private String gender;
}

Entity1

package com.limi.springboottest2.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@ConfigurationProperties(prefix = "entity1")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Entity1 {
    private Double number;
    private List<String> array;
    private User user;
    private Map<String, Integer> map;
    private String str0;
    private String str1;
    private String str2;
}

application.yml

entity1:
  number: 11
  array: ["apple", "peach", "orange"]
  user: {userName: "lily", }
  map: {"Math": 100,"English": 98,"Art": 8}
#对比字符串变量不使用引号、使用单引号、双引号的区别
  str0: \n 666
  str1: '\n 666'
  str2: "\n 666"

HelloController

package com.limi.springboottest2.controller;
import com.limi.springboottest2.entity.Entity1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
    @Autowired
    private Entity1 entity1;
    @GetMapping("/test1")
    @ResponseBody
    void test1() {
        System.out.println(entity1);
    }
}

测试结果

可以看到

  • 不使用引号和使用单引号的字符串: n 666 中的\n是直接输出\n
  • 使用双引号的字符串: \n 666 中的\n是输出为换行符

4.开启补全提示

就是下图的效果

自定义的类和配置文件绑定一般没有提示。若要提示,添加如下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
<!-- 下面插件作用是工程打包时,不将spring-boot-configuration-processor打进包内,让其只在编码的时候有用 -->
<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>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们