以前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() 便可取到值