之前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() 即可取到值

相关文章:

  • 2021-11-20
  • 2021-12-09
  • 2022-02-03
  • 2021-06-06
  • 2021-09-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-15
  • 2021-05-18
  • 2022-12-23
  • 2021-06-03
  • 2021-06-09
  • 2021-07-15
相关资源
相似解决方案