【Spring】SpringBoot配置文件

1. 配置文件格式

有下面三种:

  • application.properties
  • application.yml
  • application.yaml

其中yml是yaml的简写,两者使用方式一样,开发中yml形式用的较多。
当应⽤程序启动时, Spring Boot会⾃动从classpath路径找到并加载application.properties 和 application.yaml 或者 application.yml ⽂件。
Spring项目创建时,会自动生成一个application.properties 文件,可以使用这个,也可以自己创建其他类型配置文件。
在这里插入图片描述
注意
1.理论上讲 .properties 和 .yml 可以并存在于⼀个项⽬中,当 .properties 和 .yml并存时,两个配置都会加载. 如果配置⽂件内容有冲突, 则以.properties 为主, 也就是.properties 优先级更⾼。
2. 虽然理论上来讲 .properties 可以和 .yml 共存,但实际的业务当中,通常会采取⼀种 统⼀的配置⽂件格式,这样可以更好的维护(降低故障率)。

2. properties 配置⽂件说明

2.1 properties 基本语法

#配置端口号
server.port = 9090
#配置数据库连接信息
spring.datasource.url=spring.datasource.url=jdbc:mysql://127.0.0.1:3306/book?characterEncoding=utf8&
spring.datasource.username=root
spring.datasource.password=root

如上面代码格式,properties是以键值对形式配置的,key和value直接通过 = 连接。
注释通过#。

2.2 读取配置文件

配置文件:

#自定义配置
demo.key1 = hello,properties

@RestController
public class PropertiesController {
    @Value("${demo.key1}")
    private String key1;
    @RequestMapping("/readKey")
    public String readKey(){
        return key1;
    }
}

如上面代码,如果在项⽬中,想要主动的读取配置⽂件中的内容,可以使⽤ @Value 注解来实现。
@Value 注解使⽤" ${} "的格式读取。
执行效果:
云
如果去掉注解中 $,就会变成一个普通的赋值。
在这里插入图片描述
在这里插入图片描述

2.3 properties 缺点分析

在这里插入图片描述
如图,可以发现在配置一些文件时,代码的会有很多冗余。
这时就可以使用yml配置文件进行简化。

3. yml 配置文件说明

3.1 yml 基本语法

#配置端口号
server:
  port: 9090
#配置数据库连接信息
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/book?characterEncoding=utf8&useSSL=false
    username: root
    password: root

yml用: 代替了=,每个: 后必须有个空格后才能跟配置代码,不然无法生效。

3.2 读取配置文件

配置代码:

demo:
  key: hello,yml
@RestController
public class YmlController {
    @Value("${demo.key}")
    private String key;
    @RequestMapping("/readYml")
    public String readKey(){
        return key;
    }
}

读取规则和properties中相同,主要是yml文件中代码规则。
执行效果:
在这里插入图片描述

3.3 @PostConstruct 注解

@PostConstruct注解是Java中的一种注解,它用于在依赖注入完成后执行初始化方法。当一个Bean被创建并且所有的属性被赋值之后,该注解标注的方法会被自动调用。这样可以确保Bean在使用之前被正确初始化。
在这里插入图片描述
执行结果:
在这里插入图片描述
这样在验证一些Bean时就不用在浏览器频繁验证。
注意

  1. @PostConstruct注解的方法在同一个类中可以有多个。
  2. @PostConstruct注解的方法必须是非静态的,否则会报错。
  3. @PostConstruct注解的方法可以没有参数,也可以有参数,参数类型不限。
  4. @PostConstruct注解的方法必须是没有返回值的,包括void类型。

3.4 配置null和空格

这里是引用
执行验证:
在这里插入图片描述

3.5 value值加单双引号

demo:
  str1: Hello \n Spring
  str2: 'Hello \n Spring'
  str3: "Hello \n Spring"

在这里插入图片描述
执行结果并不同。
结论

  • 字符串默认不⽤加上单引号或者双引号。
  • 单引号会转义特殊字符,使其失去特殊功能, 始终是⼀个普通的字符串。
  • 双引号不会转义字符串⾥⾯的特殊字符, 特殊字符会表⽰本⾝的含义。

3.6 配置对象

对象Student:

@Component
@ConfigurationProperties(prefix = "student")
@Data
public class Student {
    private int age;
    private String name;
    private String sex;
}

配置对象:

# 配置对象
student:
  age: 18
  name: zhangsan
  sex: nan

执行结果:在这里插入图片描述
在对象代码中我们需要使用@ConfigurationProperties注解读取。
格式如下:
@ConfigurationProperties(prefix = "student")

3.7 配置集合

对象代码:

@Data
@ConfigurationProperties(prefix = "dbtypes")
@Component
public class Dbtype {
    private List<String> name;
}

配置代码:

# 配置集合
dbtypes:
  name:
    - chen
    - zhang
    - qian

执行结果:
在这里插入图片描述
集合读取和对象一样,但是配置文件中,集合中的元素要使用- 隔开,-后面有一个空格,如果没有空格,将变成一个元素。
读取对象也可以使用其他集合接受,也可以使用String[]等。

3.8 配置Map

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
对象读取使用Map即可。

3.9 yml优缺点

优点:

  1. 可读性⾼,写法简单, 易于理解。
  2. ⽀持更多的数据类型, 可以简单表达对象, 数组, List,Map等数据形态。
  3. ⽀持更多的编程语⾔, 不⽌是Java中可以使⽤, 在Golang, Python, Ruby, JavaScript中也可以使⽤。
    缺点:
  4. 不适合写复杂的配置⽂件。
  5. 对格式有较强的要求(⼀个空格可能会引起⼀场⾎案)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是小辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值