【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进行操作
    }
}
相关推荐
m0_74823863几秒前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
Forget the Dream22 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
jonyleek25 分钟前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
java·数据结构·算法·蓝桥杯
曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证
java·密码学
菜鸟阿达1 小时前
spring boot 2.7 + seata +微服务 降级失败问题修复
spring boot·后端·微服务
清河__1 小时前
【Go】十七、grpc 服务的具体功能编写
开发语言·后端·golang
Forget the Dream1 小时前
设计模式之代理模式
java·c++·设计模式·代理模式
张声录11 小时前
国密算法Sm2工具类--golang实现版
开发语言·后端·golang
帅的飞起来1 小时前
设计模式--spring中用到的设计模式
java·spring·设计模式