目录
1.配置⽂件
配置⽂件主要是为了解决硬编码带来的问题, 把可能会发⽣改变的信息, 放在⼀个集中的地⽅ , 当我们启 动某个程序时, 应⽤程序从配置⽂件中读取数据, 并加载运⾏.
硬编码是将数据直接嵌⼊到程序或其他可执⾏对象的源代码中, 也就是我们常说的"代码写死
使⽤配置⽂件, 可以使程序完成⽤⼾和应⽤程序的交互, 或者应⽤程序与其他应⽤程序的交互
SpringBoot⽀持并定义了配置⽂件的格式, 也在另⼀个层⾯达到了规范其他框架集成到SpringBoot的⽬的.
Spring Boot 配置⽂件有以下三种:
1.application.properties2.application.yml3.application.yaml
特殊说明:
1.理论上讲 .properties 和 .yml 可以并存在于⼀个项⽬中,当 .properties 和 .yml
并存时,两个配置都会加载. 如果配置⽂件内容有冲突, 则以 .properties 为主, 也就是.properties 优先级更⾼ .2.虽然理论上来讲 .properties 可以和 .yml 共存,但实际的业务当中,我们通常会采取⼀种
统⼀的配置⽂件格式,这样可以更好的维护(降低故障率)
2.properties 配置⽂件
properties 配置⽂件是最早期的配置⽂件格式,也是创建 SpringBoot 项⽬默认的配置⽂件
2.1properties 基本语法
properties 是以键值的形式配置的,key 和 value 之间是以"="连接的
spring提供的配置
例1 配置项⽬端⼝号
# 配置项⽬端⼝号
Server.port=9090
端口号被设置成了9090
例2 配置数据库连接信息
#配置数据库连接信息
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testdb?
characterEncoding=utf8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
properties 缺点分析:
从上述配置可以看出,properties 配置⽂件中会有很多的冗余的信息
更多配置可以参考官网:
2.2读取配置⽂件
如果在项⽬中,想要主动的读取配置⽂件中的内容,可以使⽤ @Value 注解来实现.
@Value 注解使⽤" ${} "的格式读取
用户自定义的配置
如下代码所示:
key.val = 10086
UserController类
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/User")
public class UserController {
@Value("${key.val}")
private Integer key;
@RequestMapping("/getKey")
public Integer getKey() {
return key;
}
}
运行结果:
3.yml 配置⽂件
yml 为yaml的简写, 实际开发中出现频率最⾼. yaml 和yml 的使⽤⽅式⼀样, 我们直接分析yml⽂件的 使⽤
3.1yml 基本语法
yml 是树形结构的配置⽂件,它的基础语法是"key: value"
key 和 value 之间使⽤英⽂冒号加空格的⽅式组成,空格不可省略
使⽤ yml 连接数据库:
spring:
datasource:
url: jdbc:mysql://127.0.0.0:3306/dbname?characterEncoding=utf8&useSSL=false
username: root
password: root
yml 配置读取
yml 读取配置的⽅式和 properties 完全相同
3.2yml 配置空字符串及null
#yml 配置空字符串及null
# Null,~代表null
null.value: ~
# "" 空字符串
#, 直接后⾯什么都不加就可以了, 但这种⽅式不直观, 更多的表⽰是使⽤引号括起来
empty.value: ''
3.3 value 值加单双引号
application.yml 中配置如下信息:
string:
str1: Hello \n Spring Boot.
str2: 'Hello \n Spring Boot.'
str3: "Hello \n Spring Boot"
读取程序实现代码如下:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/Yml")
public class YmlController {
@Value("${string.str1}")
private String str1;
@Value("${string.str2}")
private String str2;
@Value("${string.str3}")
private String str3;
@RequestMapping("/getStr")
public void getStr() {
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
以上程序的执⾏结果如下图所⽰:
从上述结果可以看出:
1.字符串默认不⽤加上单引号或者双引号2.单引号会转义特殊字符,使其失去特殊功能, 始终是⼀个普通的字符串3.双引号不会转义字符串⾥⾯的特殊字符, 特殊字符会表⽰本⾝的含义
3.4配置对象
我们还可以在 yml 中配置对象,如下配置:
student:
id: 1
name: wh
这个时候就不能⽤ @Value 来读取配置中的对象了,此时要使⽤另⼀个注解
@ConfigurationProperties 来读取,具体实现如下:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "student")
@Component
@Data
public class Student {
private Integer id;
private String name;
}
调⽤类 StudentController 的实现如下:
import com.wh.configuration.moder.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/get")
public class StudentController {
@Autowired
private Student student;
@RequestMapping("/Student")
public Student getStudent() {
return student;
}
}
运⾏结果如下:
3.5配置集合
配置⽂件也可以配置 list 集合,如下所⽰:
list:
name:
- wh
- ab
- cc
集合的读取和对象⼀样,也是使⽤ @ConfigurationProperties 来读取的,具体实现如下:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties("list")
@Data
public class ListConfig {
private List<String> name;
}
访问集合的实现如下:
import com.wh.configuration.moder.ListConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class ListController {
@Autowired
private ListConfig listConfig;
@RequestMapping("/readList")
public List<String> readList(){
return listConfig.getName();
}
}
运行结果:
3.6配置Map
配置⽂件也可以配置 map,如下所⽰:
maptypes:
map:
k1: v1
k2: v2
k3: v3
Map的读取和对象⼀样,也是使⽤ @ConfigurationProperties 来读取的,具体实现如下:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Map;
@Data
@Component
@ConfigurationProperties("maptypes")
public class MapConfig {
private Map<String, String> map;
}
访问Map实现如下:
import com.wh.configuration.moder.MapConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class MapController {
@Autowired
private MapConfig mapConfig;
@RequestMapping("/readMap")
public Map<String, String> getMap() {
return mapConfig.getMap();
}
}
运行结果:
3.7yml优缺点
优点:
1. 可读性⾼,写法简单, 易于理解2.⽀持更多的数据类型, 可以简单表达对象, 数组, List,Map等数据形态3.⽀持更多的编程语⾔, 不⽌是Java中可以使⽤, 在Golang, Python, Ruby, JavaScript中也可以使⽤
缺点:
1.不适合写复杂的配置⽂件
2.对格式有较强的要求(空格规范)
以上为我个人的小分享,如有问题,欢迎讨论!!!
都看到这了,不如关注一下,给个免费的赞