【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.properties、application-test.properties、application-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进行操作
    }
}
相关推荐
Highcharts.js1 小时前
成分占比饼图代码|常用可视化图表开发示例
java·开发语言·仪表盘·highcharts·实战代码·响应式图表
码事漫谈3 小时前
WorkBuddy 固然很香,但豆姐她免费呀
后端
西柚小萌新3 小时前
【人工智能:LangGraph】--3.控制流(Edge)
java·服务器·数据库
金銀銅鐵4 小时前
带您看懂字节码指令(八):数组里有什么
java·jvm·后端
zark_chuan4 小时前
怎么让 AI 写十万行代码不腐化:我的 AGENTS.md、Pre-Push 门禁与活体 Wiki
后端
r_oo_ki_e_5 小时前
牛牛与切割机
java·算法
Rain的Java大神之路6 小时前
Spring Boot 3 升级实战:真正吃掉工期的,是 MyBatis-Plus 坐标和 Security 6 重写
java·后端·面试
泡海椒6 小时前
Java PDF 报表开发:一页简历的两种版式风格对比与折行避坑
java·开发语言·pdf
wuminyu6 小时前
OpenJDK学习笔记:Native事件队列与Java回调背压
java·linux·c语言·jvm·c++
源码_空间6 小时前
基于Springboot在线考试系统(源码+文档)
java·spring boot·后端