@RefreshScope 是 Spring Cloud 中的一个注解,它用于定义配置的动态刷新。当配置属性更新时,使用 @RefreshScope 注解的 Bean 将会被自动重建,从而应用新的配置。
使用 @RefreshScope 的步骤如下:
- 在项目中引入 Spring Cloud 配置管理依赖(如 Spring Cloud Context)。
 - 在需要动态更新配置的 Bean 上添加 
@RefreshScope注解。 - 当配置更新时(如修改 Git 仓库中的配置文件),发送一个 POST 请求到 
/actuator/refresh端点。 
示例代码:
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
 
@Component
@RefreshScope
public class MyConfigurableBean {
 
    // Bean 的属性和方法
 
}当配置更新时,你可以使用如下 CURL 命令触发刷新:
curl -X POST http://localhost:8080/actuator/refresh@RefreshScope 背后的原理是 Spring 容器中的 Bean 的生命周期被自定义以支持刷新。当接收到刷新事件时,容器会销毁有 @RefreshScope 注解的 Bean,并重新创建它们,以应用新的配置。