抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

摘要:本文介绍了Spring Boot的配置管理,包括配置方式和优先级,以及如何读取配置和多环境配置。

环境

Windows 10 企业版 LTSC 21H2
Java 1.8
Maven 3.6.3
Spring 5.3.31
Spring Boot 2.7.18

1 配置方式

Spring Boot支持多种配置方式,加载顺序按优先级从高到低:

  • 命令行参数
  • 环境变量
  • 系统属性
  • 配置文件

1.1 命令行参数

启动时可以使用命令行参数:

cmd
1
java -jar demo-springboot-1.0.0-SNAPSHOT.jar --demo.name=demo-springboot

1.2 环境变量

可以使用操作系统环境变量:

cmd
1
2
set demo.name=demo-springboot
java -jar demo-springboot-1.0.0-SNAPSHOT.jar

1.3 系统属性

启动时可以使用系统属性:

cmd
1
java -Ddemo.name=demo-springboot -jar demo-springboot-1.0.0-SNAPSHOT.jar

1.4 配置文件

Spring Boot默认使用application作为配置文件名,支持Properties文件和YAML文件两种配置格式,Properties文件的优先级高于YAML文件。

1.4.1 Properties文件

示例:

properties
1
2
3
# 注释
demo.name=demo-springboot
demo.port=8080

使用占位符:

properties
1
2
demo.port=8080
server.port=${demo.port}

使用列表:

properties
1
2
demo.servers[0].ip=127.0.0.10
demo.servers[1].ip=127.0.0.11

1.4.2 YAML文件

示例:

yml
1
2
3
4
# 注释
demo:
name: demo-springboot
port: 8080

使用占位符:

yml
1
2
3
4
demo:
port: 8080
server:
port: ${demo.port}

使用列表:

yml
1
2
3
4
demo:
servers:
- ip: 127.0.0.10
- ip: 127.0.0.11

使用默认值:

yml
1
2
server:
port: ${demo.port:8080}

2 读取配置

2.1 配置值

使用@Value注解注入配置值:

java
1
2
3
4
5
6
7
8
9
10
@RestController
public class DemoController {
@Value("${demo.name}")
private String name;
@GetMapping("/demo")
public String demo() {
System.out.println(name);
return "欢迎";
}
}

2.2 环境对象

注入环境对象:

java
1
2
3
4
5
6
7
8
9
10
@RestController
public class DemoController {
@Resource
private Environment environment;
@GetMapping("/demo")
public String demo() {
System.out.println(environment.getProperty("demo.name"));
return "欢迎";
}
}

2.3 配置对象

2.3.1 使用注解

在类上使用@ConfigurationProperties注解指定前缀,将配置关联到对象的属性上。

建议导入相关依赖,增加注解提示:

pom.xml
1
2
3
4
5
6
<!-- Spring Boot Configuration Processor -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

2.3.2 注入对象

2.3.2.1 主动注入

将对象主动注入到容器中:

使用@Component注解注入到容器中:

java
1
2
3
4
5
6
7
@Component
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
private String name;
private Integer port;
// ...
}

使用@Configuration注解注入到容器中,本质上是@Component注解的特殊形式:

java
1
2
3
4
5
6
7
@Configuration
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
private String name;
private Integer port;
// ...
}

启动类无需改动,会自动扫描配置对象:

java
1
2
3
4
5
6
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

使用配置对象:

java
1
2
3
4
5
6
7
8
9
10
@RestController
public class DemoController {
@Resource
private DemoProperties demoProperties;
@GetMapping("/demo")
public String demo() {
System.out.println(demoProperties.getName());
return "欢迎";
}
}
2.3.2.2 被动注入

无需主动注入到容器中:

java
1
2
3
4
5
6
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
private String name;
private Integer port;
// ...
}

在启动类上使用注解注入配置对象:

在启动类上使用@EnableConfigurationProperties注解,指定配置对象:

java
1
2
3
4
5
6
7
@EnableConfigurationProperties(DemoProperties.class)
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

在启动类上使用@ConfigurationPropertiesScan注解,默认扫描当前包下的配置对象:

java
1
2
3
4
5
6
7
@ConfigurationPropertiesScan
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

使用配置对象:

java
1
2
3
4
5
6
7
8
9
10
@RestController
public class DemoController {
@Resource
private DemoProperties demoProperties;
@GetMapping("/demo")
public String demo() {
System.out.println(demoProperties.getName());
return "欢迎";
}
}

3 多环境配置

3.1 配置文件

使用application-环境名称作为不同环境的配置文件名:

  • application-dev:开发环境配置
  • application-prod:生产环境配置

3.2 激活方式

3.2.1 命令行参数

启动时可以使用命令行参数:

cmd
1
java -jar demo-springboot-1.0.0-SNAPSHOT.jar --spring.profiles.active=dev

3.2.2 环境变量

可以使用操作系统环境变量:

cmd
1
2
set spring.profiles.active=dev
java -jar demo-springboot-1.0.0-SNAPSHOT.jar

3.2.3 系统属性

启动时可以使用系统属性:

cmd
1
java -Dspring.profiles.active=dev -jar demo-springboot-1.0.0-SNAPSHOT.jar

3.2.4 配置文件

3.2.4.1 Properties文件

application.properties中设置

properties
1
spring.profiles.active=dev
3.2.4.2 YAML文件

application.yml中设置:

yml
1
2
3
spring:
profiles:
active: dev

评论