SpringBoot 工程下,进行统一的配置管理,你想设置的任何参数(端口号、项目根路径、数据库连接信息等等)都集中到一个固定位置和命名的配置文件(application.properties
或application.yml
)中!
配置文件应该放置在 Spring Boot 工程的src/main/resources
目录下。这是因为src/main/resources
目录是 Spring Boot 默认的类路径(classpath),配置文件会被自动加载并可供应用程序访问。
功能配置参数说明:
细节总结:
# application.properties 为统一配置文件
# 内部包含: 固定功能的key,自定义的key
# 此处的配置信息,我们都可以在程序中@Value等注解读取
# 固定的key
# 启动端口号
server.port=80
# 自定义
spring.jdbc.datasource.driverClassName=com.mysql.cj.jdbc.driver
spring.jdbc.datasource.url=jdbc:mysql:///springboot_01
spring.jdbc.datasource.username=root
spring.jdbc.datasource.password=root
package com.alex.properties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class DataSourceProperties {
@Value("${spring.jdbc.datasource.driverClassName}")
private String driverClassName;
@Value("${spring.jdbc.datasource.url}")
private String url;
@Value("${spring.jdbc.datasource.username}")
private String username;
@Value("${spring.jdbc.datasource.password}")
private String password;
// 生成get set 和 toString方法
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "DataSourceProperties{" +
"driverClassName='" + driverClassName + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
@Autowired
private DataSourceProperties dataSourceProperties;
@RequestMapping(path = "/hello")
public String sayHello() {
System.out.println(dataSourceProperties);
return "Hello Spring Boot ! ";
}
.properties
文件相比,YAML 格式有以下优势:.properties
文件不支持注释,无法提供类似的解释和说明。.properties
文件需要使用转义符或将长文本拆分为多行。.properties
文件具有更好的层次结构表示、自我描述性、注释支持、多行文本表示、复杂数据类型支持和更好的可读性。这些特点使 YAML 成为一种有力的配置文件格式,尤其适用于复杂的配置需求和人类可读的场景。然而,选择使用 YAML 还是.properties
取决于实际需求和团队的偏好,简单的配置可以使用.properties
,而复杂的配置可以选择 YAML 以获得更多的灵活性和可读性。# YAML配置文件示例
app_name: 我的应用程序
version: 1.0.0
author: 张三
database:
host: localhost
port: 5432
username: admin
password: password123
features:
- 登录
- 注册
- 仪表盘
settings:
analytics: true
theme: dark
spring:
jdbc:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql:///springboot_02
username: root
password: root
server:
port: 80
读取方式和 properties 一致
package com.alex.properties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class DataSourceProperties {
@Value("${spring.jdbc.datasource.driverClassName}")
private String driverClassName;
@Value("${spring.jdbc.datasource.url}")
private String url;
@Value("${spring.jdbc.datasource.username}")
private String username;
@Value("${spring.jdbc.datasource.password}")
private String password;
// 生成get set 和 toString方法
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "DataSourceProperties{" +
"driverClassName='" + driverClassName + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
@Autowired
private DataSourceProperties dataSourceProperties ;
@RequestMapping(path = "/hello")
public String sayHello() {
System.out.println(dataSourceProperties);
return "Hello Spring Boot ! ";
}
@ConfigurationProperties 是 SpringBoot 提供的重要注解, 他可以将一些配置属性批量注入到 bean 对象。
package com.alex.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "spring.jdbc.datasource")
public class DataSourceConfigurationProperties {
private String driverClassName;
private String url;
private String username;
private String password;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "DataSourceConfigurationProperties{" +
"driverClassName='" + driverClassName + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
@RestController
public class HelloController {
@Autowired
private DataSourceProperties dataSourceProperties;
@Autowired
private DataSourceConfigurationProperties dataSourceConfigurationProperties;
@GetMapping("/hello")
public String hello(){
System.out.println("dataSourceProperties = " + dataSourceProperties);
System.out.println("dataSourceConfigurationProperties = " + dataSourceConfigurationProperties);
return "Hello,Spring Boot 3!";
}
}
application-dev.properties
、application-prod.properties
和application-test.properties
等文件。在这些文件中,可以定义各自环境的配置参数,如数据库连接信息、端口号等。然后,在application.properties
中通过spring.profiles.active
属性指定当前使用的环境。Spring Boot 会根据该属性来加载对应环境的属性文件,覆盖默认的配置。application-dev.yml
、application-prod.yml
和application-test.yml
等文件。在这些文件中,可以使用 YAML 语法定义各自环境的配置参数。同样,通过spring.profiles.active
属性指定当前的环境,Spring Boot 会加载相应的 YAML 文件。--spring.profiles.active=dev
来指定使用开发环境的配置。创建开发、测试、生产三个环境的配置文件
spring:
jdbc:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///dev
username: root
password: root
spring:
jdbc:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///test
username: root
password: root
spring:
jdbc:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///prod
username: root
password: root
spring:
profiles:
active: dev
注意 :
如果设置了 spring.profiles.active,并且和 application 有重叠属性,以 active 设置优先。
如果设置了 spring.profiles.active,和 application 无重叠属性,application 设置依然生效!