java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring Boot读取配置文件

Spring Boot中读取配置文件的5种方式汇总(最新整理)

作者:她说..

SpringBoot提供了多种方式读取配置文件,包括@Value注解、@ConfigurationProperties、Environment接口、原生Properties/yml读取和@PropertySource加载自定义配置文件,每种方式有其适用场景和优缺点,本文介绍SpringBoot读取配置文件5种方式,感兴趣的朋友跟随小编一起看看吧

在 Spring Boot 中,读取配置文件(如 application.properties/application.yml)的方式丰富且灵活,核心可分为基础注解式编程式类型安全绑定三大类,以下是主流方式的详细总结:

一、核心前提:配置文件基础

Spring Boot 默认加载 src/main/resources 下的 application.propertiesapplication.yml,也可通过 spring.config.name/spring.config.location 自定义配置文件路径。

方式1:@Value 注解(基础单个属性读取)

核心特点

最基础、轻量的方式,直接绑定单个配置项到字段/方法参数,支持简单类型转换、默认值、SpEL 表达式。

使用示例

1. 配置文件(application.yml)

app:
  name: demo-app
  port: 8080
  desc: ${app.name}-${app.port}  # SpEL 拼接

2. 代码绑定

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class AppConfig {
    // 直接绑定单个属性
    @Value("${app.name}")
    private String appName;
    // 绑定并设置默认值(配置不存在时使用)
    @Value("${app.version:1.0.0}")
    private String appVersion;
    // 绑定 SpEL 拼接的属性
    @Value("${app.desc}")
    private String appDesc;
}

优缺点

方式2:@ConfigurationProperties(类型安全绑定,推荐)

核心特点

将一组相关配置绑定到 Java 实体类,支持嵌套对象、类型校验、前缀分组,是 Spring Boot 推荐的配置绑定方式。

使用步骤

1. 配置文件(application.yml)

app:
  name: demo-app
  port: 8080
  database:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
  servers:
    - 192.168.1.1
    - 192.168.1.2

2. 绑定实体类

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
// 前缀指定配置分组,需配合 @Component 或 @EnableConfigurationProperties 生效
@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {
    private String name;
    private Integer port;
    private Database database; // 嵌套对象
    private List<String> servers; // 集合
    // 嵌套类
    public static class Database {
        private String url;
        private String username;
        private String password;
        // getter/setter
    }
    // 必须提供 getter/setter(Spring 反射赋值)
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    // 其他字段的 getter/setter
}

3. 启用配置(可选)

若实体类未加 @Component,需在配置类中通过 @EnableConfigurationProperties 启用:

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(AppProperties.class)
public class Config {
}

增强特性

优缺点

方式3:Environment 接口(编程式读取)

核心特点

Spring 核心环境接口,可动态读取所有配置(包括系统属性、环境变量、配置文件),支持配置占位符解析。

使用示例

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class EnvConfig {
    @Autowired
    private Environment env;
    public void getConfig() {
        // 读取单个配置
        String appName = env.getProperty("app.name");
        // 读取并指定默认值
        Integer appPort = env.getProperty("app.port", Integer.class, 8080);
        // 读取嵌套配置
        String dbUrl = env.getProperty("app.database.url");
    }
}

扩展能力

优缺点

方式4:原生 Properties/yml 读取(手动加载)

核心特点

脱离 Spring 容器,手动加载配置文件(适合非 Spring 管理的代码),支持 propertiesyml 格式。

示例1:手动读取 properties 文件

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ManualPropertiesReader {
    public static void main(String[] args) throws IOException {
        Properties props = new Properties();
        // 加载 classpath 下的配置文件
        try (InputStream is = ManualPropertiesReader.class.getClassLoader().getResourceAsStream("application.properties")) {
            props.load(is);
            String appName = props.getProperty("app.name");
        }
    }
}

示例2:手动读取 yml 文件(需依赖 snakeyaml)

<!-- pom.xml 引入依赖 -->
<dependency>
    <groupId>org.yaml</groupId>
    <artifactId>snakeyaml</artifactId>
</dependency>
import org.yaml.snakeyaml.Yaml;
import java.io.InputStream;
import java.util.Map;
public class ManualYmlReader {
    public static void main(String[] args) {
        Yaml yaml = new Yaml();
        try (InputStream is = ManualYmlReader.class.getClassLoader().getResourceAsStream("application.yml")) {
            Map<String, Object> data = yaml.load(is);
            // 读取嵌套配置
            Map<String, Object> app = (Map<String, Object>) data.get("app");
            String appName = (String) app.get("name");
        }
    }
}

优缺点

方式5:@PropertySource 加载自定义配置文件

核心特点

加载非默认名称/路径的配置文件(如 custom.properties),配合 @Value@ConfigurationProperties 使用。

使用示例

1. 自定义配置文件(custom.properties)

custom.name=my-custom-app
custom.age=20

2. 加载并绑定

import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Value;
@Component
// 加载自定义 properties 文件(仅支持 properties,不直接支持 yml)
@PropertySource("classpath:custom.properties")
public class CustomConfig {
    @Value("${custom.name}")
    private String customName;
}

扩展:加载 yml 文件(需自定义工厂)

@PropertySource 原生不支持 yml,需自定义 PropertySourceFactory

import org.springframework.core.env.MapPropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.yaml.snakeyaml.Yaml;
import java.util.Map;
public class YmlPropertySourceFactory implements PropertySourceFactory {
    @Override
    public org.springframework.core.env.PropertySource<?> createPropertySource(String name, EncodedResource resource) {
        Yaml yaml = new Yaml();
        Map<String, Object> data = yaml.load(resource.getInputStream());
        return new MapPropertySource(resource.getResource().getFilename(), data);
    }
}

使用:

@PropertySource(value = "classpath:custom.yml", factory = YmlPropertySourceFactory.class)
@Component
public class CustomYmlConfig {
    @Value("${custom.name}")
    private String customName;
}

优缺点

各方式对比与选型建议

方式类型安全支持嵌套/集合配置分组手动加载推荐场景
@Value少量零散配置、简单值绑定
@ConfigurationProperties大量相关配置、类型安全场景
Environment✅(手动解析)动态读取、多环境适配、编程式
原生手动加载✅(手动解析)非 Spring 管理代码、自定义加载
@PropertySource取决于绑定方式加载自定义配置文件

选型总结

  1. 日常开发优先用 @ConfigurationProperties(类型安全、易维护);
  2. 零散简单配置用 @Value
  3. 动态读取/多环境适配用 Environment
  4. 非 Spring 代码或自定义加载逻辑用原生手动读取;
  5. 需加载自定义配置文件时结合 @PropertySource

到此这篇关于Spring Boot中读取配置文件的5种方式汇总(最新整理)的文章就介绍到这了,更多相关Spring Boot读取配置文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文