SpringBoot 配置文件

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

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

SpringBoot 配置文件

昱晟168   2022-06-28 我要评论

1.文件类型

A.properties配置文件类型

同以前properties用法一样

B.yaml

简介:

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

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

基本语法

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

注意是:字符串不需要加引号,如果加了''单引号或""双引号内容会被转义【单引号转义】或不转义【双引号不转义】

数据类型

A.字面量:

单个的,不可再分的值。date boolean string number null

K: V  #键值对之间必须有一个空格

B.对象 键值对的集合

map Object hash

#行内写法:
K: {k1:v1,k2:v2,k3:v3}
#或者
K:
    K1: v1  #键值对之间必须有一个空格
    k2: v2
    k3: v3

C.数组:一组按次排列的值。

array list set queue

#行内写法
K: [v1,v2,v3]

#或者
K:
    - v1  # `-`与`value`值一定要有一个空格
    - v2
    - v3

示例:

POJO

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Component
public class Pet {
    private String name;
    private Double weight;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@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 Set<Double> salary;
    private Map<String,List<Pet>> allPets;
}

yaml配置文件

person:
  #字面量
  username: 海康
  boss: true
  birth: 2000/11/04
  age: 21
  #对象 键值对
  pet:
    name: 阿狗
    weight: 20.28
  #数组
#  interests: [听歌,打代码,跑步] #行内写法
  interests:
    - 听歌
    - 打代码
    - 跑步
  #List 集合【和数组写法一样】
#  animal: [阿狸,阿猫,阿狗] #行内写法
  animal:
    - 阿狸
    - 阿狗
    - 阿猫
  #set集合【和数组写法一样】
#  salary: [8888.8,9999.9,28168.88] #行内写法
  salary:
    - 88988.99
    - 978988.9
    - 9999168.98
  #Map<String,Object>集合
  score:
    java: 88.8
    C++: 88.99

  #Map<String,List<Pet>> 集合
  allPets:
    haikang:
      - name: 阿狸
        weight: 20.9
      - name: 阿狗
        weight: 30.2
    iaia: [{name: 阿联,weight: 20},{name: 阿哈,weight: 21}]

controller控制器

@RestController// 表示该类是一个控制器并且只响应浏览器不进行页面跳转
public class HelloController {
    @Autowired
    Person person;
    @RequestMapping("/person")
    public Person person(){
        System.out.println(person);
        return  person;
    }
}

2.配置提示

由于在核心配置文件中,配置我们自定义配置信息【自定义的类和配置文件绑定】,IDEA没有提示

例如:上述示例一样没有提示

配置提示步骤:

步骤1:引入依赖

在pom.xml加入

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

步骤2:加入下面插件,排除在打包时,将configuration-processor的引入打包jar

在pom.xml加入

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

步骤3:重新运行RUN

例如:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
    <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 版权所有 软件发布 访问手机版

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