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
key: value
  • 对象:键值对的集合。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
1.2.4、示例
//Person类
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
//优先在properties文件中进行扫描,扫描完毕后扫描yml文件,如果properties文件中已经配置了的属性在yml文件中不会扫描,即properties文件优先级高于yml文件
@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;

}
//Pet类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Pet {
    private String name;
    private Double weight;
}

对应的yml(yaml)配置文件

Person:
  userName: xiaoming
  boss: false
  birth: 2022/5/10
  age: 18
  Pet:
    name: xiaohuang
    weight: 99.99
  interests:
    - 篮球
    - 足球
#  interests:[篮球,足球]
  animal: [tiger,bear,bee]
  score:
    math: 90
    english: 88
    computer: 100
  salary:
    - 19999
    - 29999
    - 39999
  allpets:
    sick:
      - {name: tomcat}
      - {name: mouse,weight: 66}
    height:
      - {name: bird,weight: 30}
      - {name: dog,weight: 78}
#   height:[{name: bird,weight: 30},{name: dog,weight: 78}]
{"userName":"xiaoming","boss":false,"birth":"2022-05-09T16:00:00.000+00:00","age":18,"pet":{"name":"xiaohuang","weight":99.99},"interests":["篮球","足球"],"animal":["tiger","bear","bee"],"score":{"math":90,"english":88,"computer":100},"salary":[19999.0,29999.0,39999.0],"allPets":{"sick":[{"name":"tomcat","weight":null},{"name":"mouse","weight":66.0}],"height":[{"name":"bird","weight":30.0},{"name":"dog","weight":78.0}]}}

出现问题:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
//表示数据库连接配置出现问题,因为我们在建立项目的时候选择mybatis,但是我们没有配置相应的连接属性,报错

解决方案:

  1. 将相应的依赖进行注释掉
  2. 将启动类的@SpringBootApplication修改为@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})就可以启动的时候不需要连接数据库。

1.3、JSR3数据校验

1.3.1、导入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
    <version>2.6.6</version>
</dependency>
1.3.2、添加注解

相关注解使用:https://www.jianshu.com/p/554533f88370

首先要在实体类使用@Validataed注解,在对应的属性使用相应的注解@Eamil标识该属性只能接受邮箱格式的地址,否则按照message报错

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {

    private String userName;
    private Boolean boss;
    private Date birth;
    @Email(message = "邮箱格式错误")
    private String email;
    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;

}
  email: 2532532

img

2、配置提示

在pom.xml导入下面的依赖,在编写yml配置文件的时候就有提示功能

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

为了防止将这种功能的jar包也进行打包,我们在pom.xml中添加下列代码可以防止将上述jar包打包

<configuration>
  <excludes>
    <exclude>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
    </exclude>
  </excludes>
</configuration>

3、多环境切换

profile是Spring对不同环境提供不同配置功能的支持,可以通过激活不同的环境版本,实现快速切换环境;

3.1、多配置文件

我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml , 用来指定多个环境版本;

例如:

application-test.properties 代表测试环境配置

application-dev.properties 代表开发环境配置

但是Springboot并不会直接启动这些配置文件,它默认使用application.properties主配置文件

我们需要通过一个配置来选择需要激活的环境:

#比如在配置文件中指定使用dev环境,我们可以通过设置不同的端口号进行测试;
#我们启动SpringBoot,就可以看到已经切换到dev下的配置了;
spring:
  profiles:
    active: dev		

3.1、yaml的多文档块

和properties配置文件中一样,但是使用yml去实现不需要创建多个配置文件,更加方便了 !

#多种配置中间用---隔开,使用spring:profiles:active来指定使用某个配置server:
  port: 8081

spring:
  profiles:
    active: dev
    
---
server:
  port: 8082
spring:
  profiles: dev



---
server:
  port: 8083
spring:
  profiles: test

注意:如果yml和properties同时都配置了端口,并且没有激活其他环境 , 默认会使用properties配置文件的!

3.3、配置文件加载位置

外部加载配置文件的方式十分多,我们选择最常用的即可,在开发的资源文件中进行配置!

官方外部配置文件说明参考文档

img

springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件:

优先级
1:项目路径下的config文件夹配置文件优先级
2:项目路径下配置文件优先级
3:资源路径下的config文件夹配置文件优先级
4:资源路径下配置文件
优先级由高到底,高优先级的配置会覆盖低优先级的配置;

SpringBoot会从这四个位置全部加载主配置文件;互补配置;

我们在最低级的配置文件中设置一个项目访问路径的配置来测试互补问题;

#配置项目的访问路径
server.servlet.context-path=/xxx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值