Spring Cloud--自动启用@RefreshScope功能

📅 2026/7/30 17:13:55
Spring Cloud--自动启用@RefreshScope功能
原文网址Spring Cloud--自动启用RefreshScope功能-CSDN博客简介本文手写组件自动启用RefreshScope功能。众所周知使用Nacos配置中心时将RefreshScope放到Bean的类定义上就能实现自动刷新。也就是Value注解的值可以在Nacos修改配置后自动更新不用重启服务。不加RefreshScope不影响使用只是无法在不重启的情况下更新Value的值这就会导致很多人都忘记加这个注解了。但是每个有Value的类都要加这个注解是不是太累了低端方案开发组长定时检查一下代码看谁的没有加这个注解。这实在是太low了高端方案项目启动时自动将带有Value注解的类添加RefreshScope的功能。本文采用高端方案一劳永逸所有带有Value注解的地方都会自动刷新无需添加RefreshScope。结果展示Controller代码package com.knife.example.business.product.controller; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; Api(tags 商品) RestController RequestMapping(product) public class ProductController { Value(${key1:aa}) private String key1; ApiOperation(测试) GetMapping(detail) public String detail() { return key1; } }测试访问http://localhost:8080/doc.html修改Nacos将key1改为bb不重启应用。再次请求可见已经自动更新啦代码上边是文章的部分内容为便于维护全文已转移到此地址自动启用RefreshScope功能 - 自学精灵