Spring Boot通过application.properties/yml、@Value、@ConfigurationProperties、环境变量、命令行参数等多种方式读取配置,并按优先级生效,其中命令行参数优先级最高,支持多环境Profile管理,推荐使用@ConfigurationProperties处理结构化配置以提升可维护性。

Spring Boot在读取配置方面提供了多种灵活且强大的机制,最常见且基础的包括
application.properties
application.yml
@Value
@ConfigurationProperties
Spring Boot在配置管理上的设计,在我看来,简直是神来之笔。它不仅提供了一系列开箱即用的配置方式,更重要的是,它建立了一套清晰的优先级体系,让开发者能够非常灵活地管理应用程序在不同环境下的行为。
解决方案
我们来深入聊聊Spring Boot那些读取配置的“看家本领”。
首先,最基础也是最常用的,无疑是application.properties
application.yml
.properties
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: user
password: password
driver-class-name: com.mysql.cj.jdbc.Driver而对应的
.properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=user spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
这两种文件都支持通过
application-{profile}.ymlapplication-{profile}.propertiesapplication-dev.yml
application-prod.yml
spring.profiles.active
接下来是@Value
@Value("${some.property.key}")@Component
public class MyService {
@Value("${app.name}")
private String appName;
@Value("${app.version:1.0.0}") // 可以设置默认值
private String appVersion;
public void printInfo() {
System.out.println("App Name: " + appName + ", Version: " + appVersion);
}
}@Value
@Value
那就是@ConfigurationProperties
@Component
@ConfigurationProperties(prefix = "app.settings")
public class AppSettings {
private String name;
private String description;
private List<String> features; // 支持集合类型
// Getters and Setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public List<String> getFeatures() { return features; }
public void setFeatures(List<String> features) { this.features = features; }
}然后在
application.yml
app:
settings:
name: My Awesome App
description: A powerful application for everything.
features:
- User Management
- Data Analytics
- Reporting在其他组件中,你就可以直接注入
AppSettings
@Validated
除了文件和注解,Spring Boot还会从环境变量和命令行参数中读取配置。这两种方式的优先级通常高于文件配置,非常适合在部署时动态调整某些关键参数,而无需修改和重新打包应用程序。例如,
java -jar myapp.jar --server.port=8081
application.properties
SERVER_PORT
server.port
最后,对于更高级或动态的场景,Spring Boot的Environment
PropertySource
Environment
PropertySource
理解Spring Boot配置的加载优先级是至关重要的,它决定了当同一个属性在不同地方被定义时,哪个值会最终生效。Spring Boot采用了一种非常精细且多层次的“外部化配置”策略,其优先级从高到低大致遵循以下顺序:
java -jar your-app.jar --server.port=8081
--server.port=8081
server.port
SPRING_APPLICATION_JSON
web.xml
@ServletComponentScan
web.xml
java:comp/env
java -Dkey=value
SERVER_PORT=8081
application-{profile}.propertiesapplication-{profile}.ymlspring.profiles.active=dev
application-dev.yml
application-dev.properties
application.properties
application.yml
@PropertySource
@PropertySource
这个优先级列表并非一成不变的,有些情况会略有调整,但核心思想是:离应用程序启动和运行环境越近、越动态的配置源,其优先级越高。 这使得我们可以在不修改代码和打包文件的情况下,通过调整外部环境参数来灵活控制应用程序的行为。举个例子,我通常会在
application.yml
这是一个非常常见的选择题,也是我在实际开发中经常需要权衡的地方。简单来说,它们各有优势,适用于不同的场景。
使用@Value
@Value
@Value
@Value("${key:defaultValue}")@Value
@Value("#{systemProperties['java.version']}")@Value
@Value
使用@ConfigurationProperties
@ConfigurationProperties
@Validated
@Min
@Max
@NotNull
我的个人建议是:
@Value
@ConfigurationProperties
@ConfigurationProperties
在实际的软件开发和部署中,应用程序往往需要在不同的环境(开发、测试、生产、UAT等)下运行,每个环境可能需要不同的配置,比如数据库连接、日志级别、外部服务地址等。Spring Boot提供了一套非常成熟和灵活的机制来应对这种挑战。
使用Profile(配置文件): 这是Spring Boot管理多环境配置最核心、最常用的方式。你可以在
application.yml
application.properties
application-dev.yml
application-test.yml
application-prod.yml
这些特定环境的配置文件会覆盖
application.yml
application.properties
java -jar your-app.jar --spring.profiles.active=prod
SPRING_PROFILES_ACTIVE=prod java -jar your-app.jar
java -Dspring.profiles.active=prod -jar your-app.jar
application.yml
spring:
profiles:
active: dev # 默认激活dev profile我通常会在
application.yml
application-{profile}.yml操作系统环境变量和命令行参数: 正如前面提到的,环境变量和命令行参数拥有更高的优先级。这对于在容器化部署(如Docker、Kubernetes)场景下管理配置尤为重要。你可以通过容器编排工具(如
docker-compose.yml
Deployment
environment: - SPRING_DATASOURCE_URL=jdbc:mysql://prod-db:3306/prod_db
这种方式在生产环境中非常流行,因为它将配置与应用程序代码解耦,提高了部署的灵活性和安全性。
外部化配置服务器(如Spring Cloud Config): 对于微服务架构或大型分布式系统,仅仅依靠文件和环境变量可能不够灵活。Spring Cloud Config提供了一个中心化的配置服务,可以将所有微服务的配置存储在一个Git仓库中,并提供HTTP接口供服务获取。
虽然引入Config Server会增加一些架构复杂性,但对于管理几十甚至上百个微服务的配置来说,它带来的便利性和可维护性是无与伦比的。我个人在处理大型微服务项目时,几乎总是会考虑引入Spring Cloud Config。
自定义PropertySource
PropertySource
综合来看,Spring Boot提供了从简单到复杂、从静态到动态的多层次配置管理方案。对于大多数项目,Profile结合环境变量已经足够应对;而对于大型分布式系统,引入Spring Cloud Config则能带来更高的效率和可维护性。选择哪种方式,最终还是取决于项目的规模、复杂度和团队的技术栈偏好。
以上就是Spring Boot 有哪几种读取配置的方式?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号