以前springboot项目常量类以下形式:spring
@Component @RefreshScope//nacos配置中心时添加上 public class Constants { @Value("${test1}") public String test1; }
而后在配置文件properties中写test1=123springboot
controller中应用app
@Autowired private Constants constants; spa
@GetMapping("/test") code
public String test(){ blog
logger.info("constants :{}",constants);------------------------------------------ 1 springcloud
logger.info("test nacos 配置中心 实时更新状况:{}",constants.test1);--------------- 2 get
return constants.test1;------------------------------------------------- 3 class
}test
未采用nacos做为配置中心以前都是ok的,可是采用nacos配置中心后,按照springcloud的方式配置好后,启动就出现问题了
问题是1处constants不为空,可是2,3取值均为空
解决办法:
将Constants的getter/setter添加上,而后取值采用constants.getTest1() 便可取到值