nacos作为配置中心动态刷新@RefreshScope添加后取值为null的一个问题

之前springboot项目常量类如下形式:

@Component
@RefreshScope//nacos配置中心时添加上
public class Constants {
    
    @Value("${test1}")
    public String test1; 
}

然后在配置文件properties中写test1=123

controller中应用

@Autowired private Constants constants;

@GetMapping("/test")

public String test(){

logger.info("constants :{}",constants);------------------------------------------ 1

logger.info("test nacos 配置中心 实时更新情况:{}",constants.test1);--------------- 2

return constants.test1;------------------------------------------------- 3

}

未采用nacos作为配置中心之前都是ok的,但是采用nacos配置中心后,按照springcloud的方式配置好后,启动就出现问题了

问题是1处constants不为空,但是2,3取值均为空

解决办法:

将Constants的getter/setter添加上,然后取值采用constants.getTest1() 即可取到值

猜你喜欢

转载自www.cnblogs.com/xiaoyao-001/p/11511595.html