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