Spring Cloud--@RefreshScope动态刷新的注意事项

原文网址:Spring Cloud--@RefreshScope动态刷新的注意事项_IT利刃出鞘的博客-CSDN博客

简介

本文介绍Spring Cloud的@RefreshScope动态刷新的注意事项。

不用@RefreshScope也能动态刷新

Spring Cloud的默认实现了动态刷新,不加@RefreshScope就能实现动态更新。

方法1:ApplicationContextHolder.getContext().getEnvironment().getRequiredProperty(key);

ApplicationContextHolder见:SpringBoot--静态获得Bean的工具类(基于ApplicationContext)_IT利刃出鞘的博客-CSDN博客

方法2:使用@ConfigurationProperties将配置放到类里边去

@ConfigurationProperties的用法见:SpringBoot--用类表示yml配置文件的值-CSDN博客

原理

配置属性有ConfigurationPropertiesRebinder这个监听器,监听EnvironmentChangeEvent事件。当发生EnvironmentChange事件后,会刷新Environment,然后重新构造配置类对象。

静态变量用@RefreshScope的坑

上边是文章的部分内容,为便于维护,全文已转移到此网址:Spring Cloud-@RefreshScope动态刷新的注意事项 - 自学精灵

你可能感兴趣的:(SpringCloud,spring,cloud,spring,boot,后端)