配置文件中配置了项目中重要的数据, 例如:
Spring Boot项目没有配置信息, 就不能连接和操作数据库, 甚至不能保存可以用于排查问题的关键日志. 所以配置文件非常重要.
在Spring Boot 中配置文件主要分为两种:
.properties
(主要是key=value格式).yml
(主要是key: value格式)注意:
.properties
和 .yml
, 且两个配置文件中有相同的配置项, Spring Boot 会优先考虑 .properties
, 因为 .properties
的优先级更高一些..properties
.yml
, 但是在项目中建议只使用一种配置文件的格式.properties 是以 key=value
这种格式配置的.
server.port=9090
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/2022-6-1
spring.datasource.username=root
spring.datasource.password=1234
配置文件的注释信息使用 “#”
读取配置文件的内容, 可以使用 @Value
注解来实现
@Value
注解使用 "${}"
的格式读取.
@Component public class Read implements InitializingBean { @Value("${server.port}") private String port; @Override public void afterPropertiesSet() throws Exception { System.out.println(); System.out.println(port); System.out.println(); } }
yml 是 YMAL 是缩写, 它的全称是 Yet Another Markup Language, 译为 另一种标记语言.
yml 是树形结构的配置文件, 它的基础语法是 key: value
, 这里的:
后面跟着一个空格.
server:
port: 9090
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/2022-6-1
username: root
password: 1234
# ~代表null
null.value: ~
查看一段代码
string:
str1: Hello \n World
str2: 'Hello \n World'
str3: "Hello \n World"
读取yml中的这段代码
@Component public class Read1 implements InitializingBean { @Value("${string.str1}") private String str1; @Value("${string.str2}") private String str2; @Value("${string.str3}") private String str3; @Override public void afterPropertiesSet() throws Exception { System.out.println(); System.out.println("str1: "+str1); System.out.println("str2: "+str2); System.out.println("str3: "+str3); System.out.println(); } }
运行结果:
字符串加上双引号, 会执行\n 换行.
yml 中配置对象
student:
id: 1
name: zhangsan
age: 18
读取配置的对象, 就需要用到另一个注解: @ConfigurationProperties
@Component @ConfigurationProperties("student") public class User { private int id; private String name; private int age; // 一堆getter setter }
读取
@Component public class Read2 implements InitializingBean { @Autowired private Student student; @Override public void afterPropertiesSet() throws Exception { System.out.println(); System.out.println(student); System.out.println(); } }
yml 中 配置集合
mylist:
colors:
- RED
- GREEN
- BLACK
读取配置集合
@Component @ConfigurationProperties("mylist") public class MyList { private List<String> colors; // 一堆getter 和 setter }
打印代码
@Component public class Read3 implements InitializingBean { @Autowired private MyList myList; @Override public void afterPropertiesSet() throws Exception { for (String val : myList.getColors()){ System.out.println(val); } } }
配置对象
student: {id: 1,name: zhangsan,age: 18}
配置集合
mylist: {colors: [RED,GREEN,BLACK]}
properties
的语法更复杂, yml
语法更简洁
yml通用性更好, 支持更多的语言, 如 Java, Go, Python等
yml支持更多的数据类型
yml格式的配置文件写的时候容易出错(在:之后有一个空格), 而properties写法传统比较复制,但不太容易出错
只能读取一个
@Component public class Read implements InitializingBean { @Value("${server.port}") private String port; @Override public void afterPropertiesSet() throws Exception { System.out.println(); System.out.println(port); System.out.println(); } }
直接在类上写
@Component @ConfigurationProperties("mylist") public class MyList { private List<String> colors; public List<String> getColors() { return colors; } public void setColors(List<String> colors) { this.colors = colors; } }
jdbc.username=root2
jdbc.password=root1
@Component @PropertySource(value = {"classpath:application.properties"}) public class JDBC implements InitializingBean { @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Override public void afterPropertiesSet() throws Exception { System.out.println(username + " " + password); } }