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


一、配置文件

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

1. properties配置

1.1 创建配置文件

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

1.2 添加配置项

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

1.3 在应用中使用配置项

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

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属性来指定使用的环境。

spring.profiles.active=dev

2. YAML配置

2.1 创建配置文件

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

2.2 添加配置项

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

2.3 在应用中使用配置项

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

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. 创建配置类

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. 使用配置类

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

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进行操作
    }
}
  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

武帝为此

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值