书成

再这样堕落下去就给我去死啊你这混蛋!!!

0%

SpringBoot配置文件

Spring Boot 配置文件

在 Spring Boot 中,有两种配置文件:一种是 properties,一种是 yaml。两种的区别主要在于 yaml 的数据是有序的, properties 的数据是无序的。另外, yaml 配置文件不支持 @PropertySource 注解。

配置文件位置

Spring Boot 默认有以下4个地方可以存放配置文件,优先级依次降低:

  • 当前项目根目录下的 config 目录
  • 当前项目根目录
  • resources 目录下的 config 目录
  • resources 目录

如果配置文件所在的目录或名称不是默认的,需要在项目启动时指定配置文件。

属性注入

Spring Boot 支持配置文件属性注入,新建一个 Spring Boot 项目,在 application.properties 文件中中添加如下配置:

1
2
3
book.name=aaaa
book.id=100
book.author=tt

新建一个 Book 类如下,@Component 注解将 Book 交给 Spring IoC容器管理,@Value 注解为 Book 类的字段注入配置文件中的值。

1
2
3
4
5
6
7
8
9
10
@Component
public class Book {
@Value("${book.name}")
private String name;
@Value("${book.id}")
private int id;
@Value("${book.author}")
private String author;
// 省略了其它代码....
}

通过单元测试测试注入是否成功:

1
2
3
4
5
6
7
8
9
@SpringBootTest
class PropertiesdemoApplicationTests {
@Autowired
Book book;
@Test
void contextLoads() {
System.out.println(book);
}
}

application.properties 主要存放系统配置,可以新建一个 book.properties 配置文件存放自定义的 book 配置,配置内容与原来一样,之后,在 Book 类上通过注解 @PropertySource 指定配置文件:

1
2
3
4
5
6
7
8
9
10
11
@Component
@PropertySource("classpath:book.properties")
public class Book {
@Value("${book.name}")
private String name;
@Value("${book.id}")
private int id;
@Value("${book.author}")
private String author;
// 省略了其它代码....
}

类型安全的属性注入

当属性较多时,这种一个一个属性注入的方式比较麻烦,而且容易出错,可以使用类型安全的属性注入,通过 @ConfigurationProperties 注解,指定 prefix 字段即可:

1
2
3
4
5
6
7
8
9
@Component
@PropertySource("classpath:book.properties")
@ConfigurationProperties(prefix = "book")
public class Book {
private String name;
private int id;
private String author;
// 省略了其它代码....
}

另外, @ConfigurationProperties 注解还支持JSR303数据校验,松散绑定等功能。

引入 xml 配置文件

如果想引入 xml 配置文件,可以使用 @ImportResource 注解。

在 resources 目录下新建 book.xml 文件如下:

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="book" class="top.liuzhenhui.propertiesdemo.Book" scope="prototype">
<constructor-arg name="name" value="bookName"></constructor-arg>
<constructor-arg name="author" value="bookAuthor"></constructor-arg>
<constructor-arg name="id" value="111"></constructor-arg>
</bean>
</beans>

修改 Book 类如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Book {
private String name;
private int id;
private String author;

public Book() {
}

public Book(String name, int id, String author) {
this.name = name;
this.id = id;
this.author = author;
}
// 省略了其它代码...
}

然后,在启动程序引入 xml 配置文件:

1
2
3
4
5
6
7
8
@SpringBootApplication
@ImportResource("classpath:book.xml")
public class PropertiesdemoApplication {
public static void main(String[] args) {
SpringApplication.run(PropertiesdemoApplication.class, args);
}

}

执行单元测试,发现确实已经生成了 xml 中定义的 Bean。

Profile

为了方便切换不同的开发环境,配置文件的名称可以是 application-{profile}.properties/yml,然后在 application.properties 中指定环境,项目启动时会去读取相应的配置文件。

在 resources 目录下分别创建 application-dev.properties 与 application-test.properties 如下:

1
2
3
4
5
6
7
8
9
# application-dev.properties 文件内容
book.name=devName
book.id=222
book.author=devAuthor

# application-test.properties 文件内容
book.name=testName
book.id=99
book.author=testAuthor

在启动方法上去掉之前的 @ImportResource 注解,然后修改 Book 类如下:

1
2
3
4
5
6
7
8
@Component
@ConfigurationProperties(prefix = "book")
public class Book {
private String name;
private int id;
private String author;
// 省略其它代码...
}

最后在 application.properties 文件中指定环境:

1
2
# spring.profiles.active=dev
spring.profiles.active=test

通过单元测试可以知道确实根据 application.properties 中的配置切换了读取的配置文件。