【Spring】实现自定义配置的注入和动态刷新

在Spring框架中,实现自定义配置的注入和动态刷新主要涉及以下几个步骤:

  1. 定义自定义配置类:使用@ConfigurationProperties注解来绑定配置属性。
  2. 将配置类注入到Spring上下文中:通过@Component或者@EnableConfigurationProperties注解。
  3. 实现配置的动态刷新:通常使用Spring Cloud Config或者Spring Boot Actuator来实现配置的动态刷新。

下面是一个完整的示例,演示如何实现自定义配置注入和动态刷新。

1. 定义自定义配置类

首先,定义一个配置类,并使用@ConfigurationProperties注解来绑定属性。

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "custom")
public class CustomConfig {
    private String property;

    // getter and setter
    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }
}

2. 将配置类注入到Spring上下文中

如果使用@Component注解,Spring Boot会自动将其注入到上下文中。如果没有使用@Component,可以在配置类中使用@EnableConfigurationProperties注解手动启用配置属性。

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(CustomConfig.class)
public class AppConfig {
}

3. 动态刷新配置

要实现配置的动态刷新,可以使用Spring Cloud Config和Spring Boot Actuator中的@RefreshScope注解。

首先,确保你的项目中引入了必要的依赖:

<!-- Spring Cloud Config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<!-- Spring Boot Actuator -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

然后,在需要动态刷新的地方使用@RefreshScope注解。例如:

import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ConfigController {
    private final CustomConfig customConfig;

    public ConfigController(CustomConfig customConfig) {
        this.customConfig = customConfig;
    }

    @GetMapping("/property")
    public String getProperty() {
        return customConfig.getProperty();
    }
}

4. 触发配置刷新

配置更新后,可以通过Spring Boot Actuator暴露的/actuator/refresh端点来触发配置刷新。可以使用curl命令来手动刷新:

curl -X POST http://localhost:8080/actuator/refresh

示例项目的完整代码

  1. application.yml 配置文件:
custom:
  property: "initialValue"
  1. CustomConfig.java 配置类:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "custom")
public class CustomConfig {
    private String property;

    // getters and setters
    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }
}
  1. AppConfig.java 配置启用:
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(CustomConfig.class)
public class AppConfig {
}
  1. ConfigController.java 动态刷新控制器:
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ConfigController {
    private final CustomConfig customConfig;

    public ConfigController(CustomConfig customConfig) {
        this.customConfig = customConfig;
    }

    @GetMapping("/property")
    public String getProperty() {
        return customConfig.getProperty();
    }
}
  1. Spring Boot Application 主类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

这样,通过Spring Cloud Config和Spring Boot Actuator的结合,轻松实现Spring应用中的自定义配置注入和动态刷新。

  • 24
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值