如何在 Spring Boot 中定义和读取 自定义配置

前言

在Spring Boot中定义和读取自定义配置是日常开发中常见的需求,它允许我们以灵活的方式管理应用的配置信息,无论是通过外部配置文件(如application.propertiesapplication.yml)还是通过环境变量。

作为高级程序员,我们需要掌握这一技能,以确保应用的可配置性和可维护性。以下是一个详细的步骤说明,包括示例代码,展示如何在Spring Boot中定义和读取自定义配置。

在Spring Boot中,你可以通过以下步骤定义和读取自定义配置:

  • application.propertiesapplication.yml中定义自定义配置项。

例如,在application.yml中添加:

XML 复制代码
app:
  custom:
    my-property: value
  • 创建一个配置类来绑定这些属性。
java 复制代码
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
 
@Configuration
@ConfigurationProperties(prefix = "app.custom")
public class CustomProperties {
 
    private String myProperty;
 
    public String getMyProperty() {
        return myProperty;
    }
 
    public void setMyProperty(String myProperty) {
        this.myProperty = myProperty;
    }
}
  • 在Spring Bean中注入CustomProperties类来使用配置值。
java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component
public class MyComponent {
 
    private final CustomProperties customProperties;
 
    @Autowired
    public MyComponent(CustomProperties customProperties) {
        this.customProperties = customProperties;
    }
 
    public void printCustomProperty() {
        System.out.println(customProperties.getMyProperty());
    }
}

确保@ConfigurationProperties注解的prefix属性与你在配置文件中定义的前缀相匹配。Spring Boot会自动将配置属性绑定到CustomProperties类的字段上。然后你可以在应用程序的任何部分通过自动装配的方式来使用这些配置值。

或者:

定义自定义配置属性

XML 复制代码
# application.properties
myapp.name=Spring Boot Application
myapp.description=This is a demo application for custom configuration
myapp.server.port=8081

创建配置类

接下来,我们需要创建一个配置类来绑定这些自定义属性。在Spring Boot中,这通常通过@ConfigurationProperties注解实现,它允许我们将配置文件中的属性绑定到JavaBean上。

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

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

    private String name;
    private String description;
    private int serverPort;

    // 标准的getter和setter方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getServerPort() {
        return serverPort;
    }

    public void setServerPort(int serverPort) {
        this.serverPort = serverPort;
    }
}

注意,@ConfigurationProperties(prefix = "myapp")注解指定了配置属性的前缀为myapp,这样Spring Boot就能自动将application.properties中所有以myapp开头的属性绑定到MyAppConfig类的相应字段上。

读取配置

我们可以在应用的任何地方通过依赖注入的方式读取这些配置。例如,在一个Controller中

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyAppConfigController {

    private final MyAppConfig myAppConfig;

    @Autowired
    public MyAppConfigController(MyAppConfig myAppConfig) {
        this.myAppConfig = myAppConfig;
    }

    @GetMapping("/config")
    public String getConfig() {
        return "Name: " + myAppConfig.getName() + ", Description: " + myAppConfig.getDescription() + ", Server Port: " + myAppConfig.getServerPort();
    }
}

总结

通过上述步骤,我们成功地在Spring Boot中定义了自定义配置属性,并通过@ConfigurationProperties注解将它们绑定到了JavaBean上。最后,我们通过依赖注入的方式在应用中读取这些配置。这种方式不仅提高了代码的可读性和可维护性,还使得配置管理变得更加灵活和方便。在实际开发中,合理利用Spring Boot的配置管理功能,可以大大提升应用的灵活性和可扩展性。

相关推荐
源码_V_saaskw13 分钟前
JAVA图文短视频交友+自营商城系统源码支持小程序+Android+IOS+H5
java·微信小程序·小程序·uni-app·音视频·交友
超浪的晨23 分钟前
Java UDP 通信详解:从基础到实战,彻底掌握无连接网络编程
java·开发语言·后端·学习·个人开发
双力臂4041 小时前
Spring Boot 单元测试进阶:JUnit5 + Mock测试与切片测试实战及覆盖率报告生成
java·spring boot·后端·单元测试
♡喜欢做梦1 小时前
【MySQL】深入浅出事务:保证数据一致性的核心武器
数据库·mysql
遇见你的雩风1 小时前
MySQL的认识与基本操作
数据库·mysql
Edingbrugh.南空1 小时前
Aerospike与Redis深度对比:从架构到性能的全方位解析
java·开发语言·spring
dblens 数据库管理和开发工具1 小时前
MySQL新增字段DDL:锁表全解析、避坑指南与实战案例
数据库·mysql·dblens·dblens mysql·数据库连接管理
weixin_419658311 小时前
MySQL的基础操作
数据库·mysql
QQ_4376643142 小时前
C++11 右值引用 Lambda 表达式
java·开发语言·c++
永卿0012 小时前
设计模式-迭代器模式
java·设计模式·迭代器模式