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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM