【Spring Boot 自定义配置项详解】

文章目录

  • 一、配置文件
    • [1. properties配置](#1. properties配置)
      • [1.1 创建配置文件](#1.1 创建配置文件)
      • [1.2 添加配置项](#1.2 添加配置项)
      • [1.3 在应用中使用配置项](#1.3 在应用中使用配置项)
      • [1.4 多环境配置](#1.4 多环境配置)
    • [2. YAML配置](#2. YAML配置)
      • [2.1 创建配置文件](#2.1 创建配置文件)
      • [2.2 添加配置项](#2.2 添加配置项)
      • [2.3 在应用中使用配置项](#2.3 在应用中使用配置项)
      • [2.4 多环境配置](#2.4 多环境配置)
  • 二、自定义配置类
    • [1. 创建配置类](#1. 创建配置类)
    • [2. 使用配置类](#2. 使用配置类)

一、配置文件

Spring Boot支持多种配置文件格式,包括properties和YAML。可以根据项目需求选择合适的格式。

1. properties配置

1.1 创建配置文件

在src/main/resources目录下创建一个application.properties文件(如果没有则新建)。这是Spring Boot默认读取的配置文件名。

1.2 添加配置项

properties 复制代码
# 数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=root
spring.datasource.password=password

1.3 在应用中使用配置项

通过@Value注解或@ConfigurationProperties注解将配置值注入到Spring Bean中:

java 复制代码
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class DatabaseConfig {

    @Value("${spring.datasource.url}")
    private String dbUrl;

    @Value("${spring.datasource.username}")
    private String dbUsername;

    @Value("${spring.datasource.password}")
    private String dbPassword;

    // 省略getter和setter方法
}

1.4 多环境配置

可以根据不同的环境(如开发、测试、生产)创建不同的配置文件(如application-dev.propertiesapplication-test.propertiesapplication-prod.properties),并在启动应用程序时通过spring.profiles.active属性来指定使用的环境。

properties 复制代码
spring.profiles.active=dev

2. YAML配置

2.1 创建配置文件

创建一个application.yml文件,YAML格式的配置文件更易读且支持复杂结构的配置。

2.2 添加配置项

yaml 复制代码
# 数据库连接配置
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydatabase
    username: root
    password: password

2.3 在应用中使用配置项

与properties配置类似,通过@ConfigurationProperties注解将配置值注入到Spring Bean中。

java 复制代码
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class DatabaseConfig {

    private String url;
    private String username;
    private String password;

    // 省略getter和setter方法
}

2.4 多环境配置

同样支持多环境配置,可以使用spring.profiles.active属性来指定不同的环境。


二、自定义配置类

除了使用@Value@ConfigurationProperties注解外,还可以通过自定义配置类的方式来管理配置项。

1. 创建配置类

java 复制代码
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "myapp")
public class MyAppConfig {

    private String apiUrl;
    private String apiKey;

    // 省略getter和setter方法
}

2. 使用配置类

直接在需要使用的地方注入该配置类即可:

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyApiClient {

    private final MyAppConfig appConfig;

    @Autowired
    public MyApiClient(MyAppConfig appConfig) {
        this.appConfig = appConfig;
    }

    public void callApi() {
        String apiUrl = appConfig.getApiUrl();
        String apiKey = appConfig.getApiKey();

        // 使用配置的API URL和API Key进行操作
    }
}
相关推荐
未若君雅裁14 分钟前
LeetCode 51 - N皇后问题 详解笔记
java·数据结构·笔记·算法·leetcode·剪枝
码事漫谈27 分钟前
Visual Studio 2026真的值得升级吗中国开发者实测报告
后端
失散1338 分钟前
架构师级别的电商项目——2 电商项目核心需求分析
java·分布式·微服务·架构·需求分析
麦兜*40 分钟前
Redis内存消耗异常飙升?深入排查与Big Key/Hot Key的根治方案
jvm·数据库·spring boot·redis·spring·缓存
MediaTea1 小时前
Python 第三方库:Flask(轻量级 Web 框架)
开发语言·前端·后端·python·flask
Kuo-Teng1 小时前
LeetCode 198: House Robber
java·算法·leetcode·职场和发展·动态规划
q***72561 小时前
Spring Boot + Vue 全栈开发实战指南
vue.js·spring boot·后端
小七mod2 小时前
【Spring】Spring Boot自动配置的案例
java·spring boot·spring·自动配置·源码·ioc·aop
java干货2 小时前
Spring Boot 为什么“抛弃”了 spring.factories?
spring boot·python·spring
红石榴花生油2 小时前
Docker + Nginx 部署 Java 项目(JAR 包 + WAR 包)实战笔记
java·tomcat·maven