SpringBoot 配置⽂件
目录
1.配置⽂件
2.properties 配置⽂件
2.1properties 基本语法
2.2读取配置⽂件
3.yml 配置⽂件
3.1yml 基本语法
3.2yml 配置空字符串及null
3.3 value 值加单双引号
3.4配置对象
3.5配置集合
3.6配置Map
3.7yml优缺点
1.配置⽂件
硬编码是将数据直接嵌⼊到程序或其他可执⾏对象的源代码中, 也就是我们常说的"代码写死
使⽤配置⽂件, 可以使程序完成⽤⼾和应⽤程序的交互, 或者应⽤程序与其他应⽤程序的交互
1.application.properties2.application.yml3.application.yaml
特殊说明:
1.理论上讲 .properties 和 .yml 可以并存在于⼀个项⽬中,当 .properties 和 .yml
并存时,两个配置都会加载. 如果配置⽂件内容有冲突, 则以 .properties 为主, 也就是.properties 优先级更⾼ .2.虽然理论上来讲 .properties 可以和 .yml 共存,但实际的业务当中,我们通常会采取⼀种
统⼀的配置⽂件格式,这样可以更好的维护(降低故障率)
2.properties 配置⽂件
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 配置⽂件中会有很多的冗余的信息
更多配置可以参考官网:
Common Application Properties :: Spring Boothttps://docs.spring.io/spring-boot/appendix/application-properties/index.html
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 配置⽂件
3.1yml 基本语法
yml 是树形结构的配置⽂件,它的基础语法是"key: value"
key 和 value 之间使⽤英⽂冒号加空格的⽅式组成,空格不可省略
使⽤ yml 连接数据库:
spring:datasource:url: jdbc:mysql://127.0.0.0:3306/dbname?characterEncoding=utf8&useSSL=falseusername: rootpassword: root
yml 读取配置的⽅式和 properties 完全相同
3.2yml 配置空字符串及null
#yml 配置空字符串及null# Null,~代表null
null.value: ~# "" 空字符串
#, 直接后⾯什么都不加就可以了, 但这种⽅式不直观, 更多的表⽰是使⽤引号括起来
empty.value: ''
3.3 value 值加单双引号
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: 1name: wh
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;
}
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 {@Autowiredprivate Student student;@RequestMapping("/Student")public Student getStudent() {return student;}
}

3.5配置集合
list:name:- wh- ab- cc
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 {@Autowiredprivate ListConfig listConfig;@RequestMapping("/readList")public List<String> readList(){return listConfig.getName();}
}
运行结果:
3.6配置Map
maptypes:map:k1: v1k2: v2k3: v3
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 {@Autowiredprivate 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.对格式有较强的要求(空格规范)
以上为我个人的小分享,如有问题,欢迎讨论!!!
都看到这了,不如关注一下,给个免费的赞