
在 SpringBoot 开发中,配置读取是连接外部环境与业务逻辑的关键桥梁。无论是数据库连接信息、第三方 API 密钥,还是业务参数,都需要通过配置机制灵活注入应用。但你真的掌握了所有配置读取方式吗?为什么同样的配置在不同场景下表现不同?本文将从基础到进阶,全面解析 SpringBoot 的 7 种配置读取方式,结合底层原理和实战案例,让你既能解决当下问题,又能理解背后逻辑。
在深入具体读取方式前,我们先理清 SpringBoot 的配置体系。理解这些基础,能帮你在实际开发中避开 80% 的配置问题。
SpringBoot 支持两种主流配置文件格式:
user.name=kenuser:
name: ken
age: 30
两者的核心区别在于语法结构:properties 用.分隔层级,yml 用缩进表示层级(注意缩进必须用空格,不能用 Tab)。
SpringBoot 会从多个位置加载配置,优先级从高到低如下(高优先级配置会覆盖低优先级):
java -jar app.jar --user.name=ken)application-{profile}.properties/yml(激活的环境配置)application.properties/yml(默认配置)config目录(classpath:config/)classpath:/)SpringBoot 的配置最终会被抽象为PropertySource对象,所有PropertySource被聚合到Environment中,形成一个统一的配置查询入口。这个过程可以用流程图表示:

@Value通过 Spring 的BeanPostProcessor机制,在 Bean 初始化时解析${}表达式,从Environment中获取对应配置并注入字段。支持 SpEL 表达式(#{}),可实现简单的计算或逻辑。
步骤 1:添加配置(application.yml)
app:
name: spring-config-demo
version: 1.0.0
enable-feature: true
timeout: 3000
supported-types: json,xml,csv
max-connections: 100
步骤 2:创建配置读取类
package com.ken.configdemo.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.Arrays;
/**
* 演示@Value注解读取配置
*
* @author ken
*/
@RestController
@RequestMapping("/value-demo")
@Slf4j
@Tag(name = "@Value配置读取示例", description = "展示@Value注解的各种用法")
public class ValueDemoController {
/**
* 注入字符串类型配置
*/
@Value("${app.name}")
private String appName;
/**
* 注入整数类型配置
*/
@Value("${app.version}")
private String appVersion;
/**
* 注入布尔类型配置
*/
@Value("${app.enable-feature:false}")
private boolean enableFeature;
/**
* 注入长整数类型配置
*/
@Value("${app.timeout}")
private Long timeout;
/**
* 注入数组(通过逗号分隔的字符串转换)
*/
@Value("${app.supported-types}")
private String[] supportedTypes;
/**
* 使用SpEL表达式计算(max-connections的1/2)
*/
@Value("#{${app.max-connections} / 2}")
private Integer halfConnections;
/**
* 展示所有通过@Value注入的配置
*
* @return 配置信息
*/
@GetMapping
@Operation(summary = "获取@Value注入的配置", description = "返回所有通过@Value注解读取的配置值")
public String showConfig() {
log.info("应用名称:{}", appName);
log.info("支持的类型:{}", Arrays.toString(supportedTypes));
return String.format(
"appName: %s, appVersion: %s, enableFeature: %s, timeout: %d, supportedTypes: %s, halfConnections: %d",
appName, appVersion, enableFeature, timeout, Arrays.toString(supportedTypes), halfConnections
);
}
}
String→Long、boolean)#{}使用表达式,如#{T(java.lang.Math).random()}IllegalArgumentException@ConfigurationProperties通过绑定规则(默认按字段名匹配配置键),将一组相关配置批量注入到 Bean 的字段中。支持复杂类型(如嵌套对象、集合),且提供配置校验能力。
步骤 1:添加配置(application.yml)
database:
primary:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/main_db?useSSL=false&serverTimezone=UTC
username: root
password: root123
pool:
max-active: 20
max-idle: 10
min-idle: 5
test-query: SELECT 1
secondary:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/second_db?useSSL=false&serverTimezone=UTC
username: root
password: root123
步骤 2:创建配置类
package com.ken.configdemo.config;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
/**
* 数据库配置属性类
* 通过@ConfigurationProperties绑定database前缀的配置
*
* @author ken
*/
@Data
@ConfigurationProperties(prefix = "database")
@Validated // 启用配置校验
public class DatabaseProperties {
/**
* 主数据库配置
*/
private DbConfig primary;
/**
* 从数据库配置
*/
private DbConfig secondary;
/**
* 数据库连接配置内部类
*/
@Data
public static class DbConfig {
/**
* 驱动类名(不能为空)
*/
@NotBlank(message = "数据库驱动类名不能为空")
private String driverClassName;
/**
* 连接URL(不能为空)
*/
@NotBlank(message = "数据库连接URL不能为空")
private String url;
/**
* 用户名(不能为空)
*/
@NotBlank(message = "数据库用户名不能为空")
private String username;
/**
* 密码
*/
private String password;
/**
* 连接池配置
*/
private PoolConfig pool;
/**
* 连接池配置内部类
*/
@Data
public static class PoolConfig {
/**
* 最大活跃连接数(最小值为1)
*/
@Min(value = 1, message = "最大活跃连接数不能小于1")
private Integer maxActive;
/**
* 最大空闲连接数
*/
private Integer maxIdle;
/**
* 最小空闲连接数
*/
private Integer minIdle;
/**
* 测试查询语句
*/
private String testQuery;
}
}
}
步骤 3:启用配置绑定在启动类添加@ConfigurationPropertiesScan注解,扫描配置类:
package com.ken.configdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
@SpringBootApplication
@ConfigurationPropertiesScan("com.ken.configdemo.config") // 扫描配置类所在包
public class ConfigDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigDemoApplication.class, args);
}
}
步骤 4:使用配置
package com.ken.configdemo.controller;
import com.ken.configdemo.config.DatabaseProperties;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 演示@ConfigurationProperties的使用
*
* @author ken
*/
@RestController
@RequestMapping("/config-properties-demo")
@Slf4j
@Tag(name = "@ConfigurationProperties示例", description = "展示批量配置绑定的用法")
@RequiredArgsConstructor // 生成构造器注入
public class ConfigPropertiesDemoController {
/**
* 注入数据库配置(通过构造器注入,Spring推荐方式)
*/
private final DatabaseProperties databaseProperties;
/**
* 展示数据库配置信息
*
* @return 配置详情
*/
@GetMapping("/primary-db")
@Operation(summary = "获取主数据库配置", description = "返回主数据库的连接信息和连接池配置")
public String showPrimaryDbConfig() {
DatabaseProperties.DbConfig primary = databaseProperties.getPrimary();
DatabaseProperties.DbConfig.PoolConfig pool = primary.getPool();
log.info("主数据库URL:{}", primary.getUrl());
log.info("主数据库连接池最大活跃数:{}", pool.getMaxActive());
return String.format(
"主库配置:URL=%s, 用户名=%s, 最大连接数=%d",
primary.getUrl(), primary.getUsername(), pool.getMaxActive()
);
}
}
prefix指定配置前缀,自动匹配字段(支持驼峰与短横线转换,如max-active匹配maxActive)DatabaseProperties→DbConfig→PoolConfig)@Validated和 JSR-303 注解(如@NotBlank、@Min)实现配置合法性校验特性 | @Value | @ConfigurationProperties |
|---|---|---|
批量注入 | 不支持(需逐个标注) | 支持(通过前缀批量绑定) |
复杂类型 | 不支持嵌套对象 | 支持嵌套对象、集合 |
配置校验 | 不支持 | 支持(结合 @Validated) |
松绑定 | 不支持(严格匹配) | 支持(驼峰 / 短横线转换) |
默认值 | 支持(通过 ${key:default}) | 支持(直接给字段赋值) |
Environment是 Spring 的核心接口,聚合了所有PropertySource,提供统一的配置查询方法。它不仅能获取配置值,还能获取当前激活的环境(profile)等信息。
步骤 1:添加配置(application.yml)
spring:
profiles:
active: dev
cache:
type: redis
redis:
expiration: 3600
host: localhost
port: 6379
步骤 2:使用 Environment 查询配置
package com.ken.configdemo.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.StringUtils;
/**
* 演示Environment接口读取配置
*
* @author ken
*/
@RestController
@RequestMapping("/environment-demo")
@Slf4j
@Tag(name = "Environment示例", description = "展示通过Environment接口读取配置的用法")
@RequiredArgsConstructor
public class EnvironmentDemoController {
/**
* 注入Environment接口
*/
private final Environment environment;
/**
* 获取缓存配置及当前环境信息
*
* @return 配置信息
*/
@GetMapping("/cache-info")
@Operation(summary = "获取缓存配置", description = "通过Environment获取缓存配置和当前激活的环境")
public String getCacheInfo() {
// 获取简单配置
String cacheType = environment.getProperty("cache.type");
// 获取整数配置(指定默认值)
Integer redisPort = environment.getProperty("cache.redis.port", Integer.class, 6379);
// 获取长整数配置
Long expiration = environment.getProperty("cache.redis.expiration", Long.class);
// 获取当前激活的环境
String[] activeProfiles = environment.getActiveProfiles();
String activeProfilesStr = StringUtils.arrayToCommaDelimitedString(activeProfiles);
log.info("当前激活环境:{}", activeProfilesStr);
log.info("缓存类型:{},Redis端口:{}", cacheType, redisPort);
return String.format(
"激活环境:%s, 缓存类型:%s, Redis地址:%s:%d, 过期时间:%ds",
activeProfilesStr,
cacheType,
environment.getProperty("cache.redis.host"),
redisPort,
expiration
);
}
}
getProperty(String key):获取字符串类型配置getProperty(String key, Class<T> type):获取指定类型的配置getProperty(String key, Class<T> type, T defaultValue):带默认值的类型转换getActiveProfiles():获取当前激活的环境containsProperty(String key):判断配置是否存在@PropertySource用于加载指定路径的自定义配置文件(默认支持 properties 格式),加载的配置会被添加到Environment中,可通过@Value或@ConfigurationProperties读取。
步骤 1:创建自定义配置文件(classpath:config/app-features.properties)
# 自定义功能配置
feature.payment.enable=true
feature.payment.supported-methods=alipay,wechat,unionpay
feature.notify.url=http://localhost:8080/notify
feature.rate.limit=100
步骤 2:加载自定义配置文件
package com.ken.configdemo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* 加载自定义配置文件
*
* @author ken
*/
@Configuration
@PropertySource(value = "classpath:config/app-features.properties", encoding = "UTF-8")
// 若需要加载多个文件,可使用数组:@PropertySource(value = {"file:./config/ext.properties", "classpath:app.properties"})
public class CustomConfigLoader {
}
步骤 3:读取自定义配置
package com.ken.configdemo.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.StringUtils;
import java.util.Arrays;
/**
* 演示@PropertySource加载自定义配置
*
* @author ken
*/
@RestController
@RequestMapping("/property-source-demo")
@Slf4j
@Tag(name = "@PropertySource示例", description = "展示加载自定义配置文件的用法")
@RequiredArgsConstructor
public class PropertySourceDemoController {
@Value("${feature.payment.enable}")
private boolean paymentEnabled;
@Value("${feature.payment.supported-methods}")
private String[] supportedPaymentMethods;
@Value("${feature.notify.url}")
private String notifyUrl;
@Value("${feature.rate.limit}")
private Integer rateLimit;
/**
* 展示自定义配置文件中的配置
*
* @return 配置信息
*/
@GetMapping
@Operation(summary = "获取自定义配置", description = "返回通过@PropertySource加载的配置")
public String showCustomConfig() {
log.info("支付功能是否启用:{}", paymentEnabled);
log.info("支持的支付方式:{}", Arrays.toString(supportedPaymentMethods));
return String.format(
"支付功能:%s, 支持方式:%s, 通知地址:%s, 限流阈值:%d",
paymentEnabled ? "启用" : "禁用",
StringUtils.arrayToCommaDelimitedString(supportedPaymentMethods),
notifyUrl,
rateLimit
);
}
}
@PropertySource默认不支持 YAML 文件,需自定义PropertySourceFactory:
步骤 1:创建 YAML 配置工厂
package com.ken.configdemo.config;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.lang.Nullable;
import java.io.IOException;
import java.util.List;
/**
* 支持YAML文件的PropertySourceFactory
*
* @author ken
*/
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
// 使用SpringBoot的YamlPropertySourceLoader加载YAML文件
YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
List<PropertySource<?>> sources = loader.load(resource.getResource().getFilename(), resource.getResource());
return sources.get(0);
}
}
步骤 2:加载 YAML 文件(classpath:config/third-party.yml)
third-party:
oss:
endpoint: oss-cn-beijing.aliyuncs.com
access-key: LTAI4Gxxxxxxxxx
secret-key: 8hxxxxxxxxxxxxxxxxxxxx
bucket-name: my-bucket
步骤 3:通过 @PropertySource 加载
package com.ken.configdemo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* 加载YAML格式的自定义配置文件
*
* @author ken
*/
@Configuration
@PropertySource(
value = "classpath:config/third-party.yml",
factory = YamlPropertySourceFactory.class, // 指定自定义工厂
encoding = "UTF-8"
)
public class YamlConfigLoader {
}
file:前缀)classpath:(类路径)和file:(文件系统)PropertySourceFactory构造器注入是 Spring 推荐的依赖注入方式,通过构造方法将配置值注入到 Bean 中。相比字段注入,它能确保 Bean 在实例化时就完成配置注入,避免NullPointerException。
步骤 1:添加配置(application.yml)
message:
welcome: "欢迎使用SpringBoot配置示例"
max-length: 200
enabled: true
步骤 2:通过构造器注入配置
package com.ken.configdemo.service;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
/**
* 演示构造器注入配置
*
* @author ken
*/
@Service
@Slf4j
@Getter // 提供getter方法
public class MessageService {
/**
* 欢迎消息
*/
private final String welcomeMessage;
/**
* 消息最大长度
*/
private final Integer maxLength;
/**
* 是否启用消息功能
*/
private final boolean enabled;
/**
* 通过构造器注入配置
*
* @param welcomeMessage 欢迎消息
* @param maxLength 消息最大长度
* @param enabled 是否启用
*/
public MessageService(
@Value("${message.welcome}") String welcomeMessage,
@Value("${message.max-length}") Integer maxLength,
@Value("${message.enabled:true}") boolean enabled) {
// 校验配置合法性
if (!StringUtils.hasText(welcomeMessage)) {
throw new IllegalArgumentException("欢迎消息不能为空");
}
if (maxLength == null || maxLength <= 0) {
throw new IllegalArgumentException("消息最大长度必须大于0");
}
this.welcomeMessage = welcomeMessage;
this.maxLength = maxLength;
this.enabled = enabled;
log.info("MessageService初始化完成,欢迎消息:{}", welcomeMessage);
}
/**
* 处理消息(截断超长消息)
*
* @param content 消息内容
* @return 处理后的消息
*/
public String processMessage(String content) {
if (!enabled) {
return "消息功能已禁用";
}
if (content.length() > maxLength) {
return content.substring(0, maxLength) + "...";
}
return content;
}
}
步骤 3:使用服务
package com.ken.configdemo.controller;
import com.ken.configdemo.service.MessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 演示构造器注入配置的使用
*
* @author ken
*/
@RestController
@RequestMapping("/constructor-inject-demo")
@Slf4j
@Tag(name = "构造器注入示例", description = "展示通过构造器注入配置的最佳实践")
@RequiredArgsConstructor
public class ConstructorInjectDemoController {
private final MessageService messageService;
/**
* 处理消息
*
* @param content 消息内容
* @return 处理结果
*/
@GetMapping("/process")
@Operation(summary = "处理消息", description = "根据配置处理消息内容(截断超长消息)")
public String processMessage(@RequestParam String content) {
return messageService.processMessage(content);
}
/**
* 获取欢迎消息
*
* @return 欢迎消息
*/
@GetMapping("/welcome")
@Operation(summary = "获取欢迎消息", description = "返回通过构造器注入的欢迎消息")
public String getWelcomeMessage() {
return messageService.getWelcomeMessage();
}
}
final,确保注入后不可修改在分布式系统中,单机配置文件无法满足多实例同步更新的需求,配置中心(如 Nacos、Apollo)成为最佳选择。SpringBoot 可无缝集成配置中心,实现配置的动态更新。
步骤 1:添加依赖(pom.xml)
<!-- Nacos配置中心依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2023.0.1.0</version>
</dependency>
<!-- SpringCloud上下文依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>4.1.1</version>
</dependency>
步骤 2:配置 Nacos 地址(bootstrap.yml,优先级高于 application.yml)
spring:
application:
name: config-demo # 应用名,用于匹配Nacos中的配置
cloud:
nacos:
config:
server-addr: localhost:8848 # Nacos服务器地址
file-extension: yml # 配置文件格式
group: DEFAULT_GROUP # 配置分组
namespace: public # 命名空间(默认public)
步骤 3:在 Nacos 控制台添加配置
distributed:
config:
enable-monitor: true
refresh-interval: 5000
server-list: 192.168.1.100:8080,192.168.1.101:8080
步骤 4:读取 Nacos 配置(支持动态更新)
package com.ken.configdemo.controller;
import com.alibaba.fastjson2.JSON;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* 演示Nacos配置中心的使用
*
* @author ken
*/
@RestController
@RequestMapping("/nacos-config-demo")
@Slf4j
@Tag(name = "Nacos配置中心示例", description = "展示分布式环境下通过配置中心读取配置")
@RequiredArgsConstructor
@RefreshScope // 启用配置动态刷新
public class NacosConfigDemoController {
@Value("${distributed.config.enable-monitor:false}")
private boolean enableMonitor;
@Value("${distributed.config.refresh-interval:3000}")
private Integer refreshInterval;
@Value("${distributed.config.server-list}")
private String[] serverList;
/**
* 获取分布式配置
*
* @return 配置信息
*/
@GetMapping
@Operation(summary = "获取分布式配置", description = "返回从Nacos配置中心读取的配置,支持动态更新")
public String getDistributedConfig() {
Map<String, Object> configMap = new HashMap<>(3);
configMap.put("enableMonitor", enableMonitor);
configMap.put("refreshInterval", refreshInterval);
configMap.put("serverList", Arrays.toString(serverList));
log.info("分布式配置:{}", JSON.toJSONString(configMap));
return JSON.toJSONString(configMap);
}
}
@RefreshScope注解会为 Bean 创建代理对象,当配置更新时,Spring 会销毁旧的 Bean 实例,创建新实例并注入最新配置。流程如下:

在某些场景(如工具类、非 Spring 管理的类)中,无法直接使用 Spring 的注解注入配置,此时需要自定义工具类读取配置。
步骤 1:创建配置工具类
package com.ken.configdemo.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
/**
* 配置读取工具类(支持非Spring管理的类调用)
*
* @author ken
*/
@Component
@Slf4j
public class ConfigUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/**
* 设置ApplicationContext(Spring自动调用)
*
* @param context 应用上下文
*/
@Override
public void setApplicationContext(ApplicationContext context) {
if (applicationContext == null) {
applicationContext = context;
log.info("ConfigUtils初始化完成");
}
}
/**
* 获取Environment对象
*
* @return Environment实例
*/
private static Environment getEnvironment() {
if (applicationContext == null) {
throw new IllegalStateException("Spring上下文未初始化,无法获取配置");
}
return applicationContext.getEnvironment();
}
/**
* 获取字符串类型配置
*
* @param key 配置键
* @return 配置值(不存在返回null)
*/
public static String getString(String key) {
return getEnvironment().getProperty(key);
}
/**
* 获取字符串类型配置(带默认值)
*
* @param key 配置键
* @param defaultValue 默认值
* @return 配置值(不存在返回默认值)
*/
public static String getString(String key, String defaultValue) {
String value = getEnvironment().getProperty(key);
return StringUtils.hasText(value) ? value : defaultValue;
}
/**
* 获取整数类型配置
*
* @param key 配置键
* @return 配置值(不存在或转换失败返回null)
*/
public static Integer getInteger(String key) {
return getEnvironment().getProperty(key, Integer.class);
}
/**
* 获取整数类型配置(带默认值)
*
* @param key 配置键
* @param defaultValue 默认值
* @return 配置值(不存在或转换失败返回默认值)
*/
public static Integer getInteger(String key, Integer defaultValue) {
return getEnvironment().getProperty(key, Integer.class, defaultValue);
}
/**
* 获取布尔类型配置
*
* @param key 配置键
* @param defaultValue 默认值
* @return 配置值(不存在返回默认值)
*/
public static boolean getBoolean(String key, boolean defaultValue) {
return getEnvironment().getProperty(key, Boolean.class, defaultValue);
}
}
步骤 2:在非 Spring 管理的类中使用
package com.ken.configdemo.util;
import lombok.extern.slf4j.Slf4j;
/**
* 非Spring管理的工具类(演示如何使用ConfigUtils)
*
* @author ken
*/
@Slf4j
public class CommonUtils {
/**
* 生成业务编号
*
* @return 业务编号
*/
public static String generateBizNo() {
// 从配置获取前缀
String prefix = ConfigUtils.getString("biz.no.prefix", "BIZ");
// 从配置获取长度
int length = ConfigUtils.getInteger("biz.no.length", 16);
log.info("生成业务编号,前缀:{},长度:{}", prefix, length);
// 简化的编号生成逻辑(实际项目需更复杂实现)
return prefix + System.currentTimeMillis() % (long) Math.pow(10, length - prefix.length());
}
}
步骤 3:添加配置(application.yml)
biz:
no:
prefix: ORDER
length: 20
步骤 4:在控制器中使用
package com.ken.configdemo.controller;
import com.ken.configdemo.util.CommonUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 演示自定义配置工具类的使用
*
* @author ken
*/
@RestController
@RequestMapping("/config-utils-demo")
@Slf4j
@Tag(name = "配置工具类示例", description = "展示在非Spring管理类中读取配置")
public class ConfigUtilsDemoController {
/**
* 生成业务编号
*
* @return 业务编号
*/
@GetMapping("/generate-biz-no")
@Operation(summary = "生成业务编号", description = "通过自定义工具类读取配置生成业务编号")
public String generateBizNo() {
return CommonUtils.generateBizNo();
}
}
ApplicationContext为 null)@RefreshScope和事件监听)@Value@ConfigurationPropertiesEnvironment@PropertySourcedatabase.yml、cache.yml),避免单一文件过大@Validated确保配置合法性,提前暴露问题SpringBoot 提供了丰富的配置读取方式,每种方式都有其适用场景:从简单的@Value到批量绑定的@ConfigurationProperties,从底层的Environment到分布式的配置中心,再到自定义工具类,形成了完整的配置生态。