Bug:引入Feign后发现监控onApplication中ContextRefreshedEvent事件触发了2次或者4次。
【原理】在Spring的文档注释中提示到: Event raised when an {@code ApplicationContext} gets initialized or refreshed.即当 ApplicationContext 进行初始化或者刷新时都会发送该事件。只有 finishRefresh() 方法里面会发送该事件,而该方法又只有 refresh() 方法调用,在 refresh() 方法中有这些注释: Load or refresh the persistent representation of the configuration, which ight be from Java-based configuration, an XML file, a properties file, a elational database schema, or some other format. As this is a startup method, it should destroy already created singletons if it fails, to avoid dangling resources. In other words, after invocation of this method, either all or no singletons at all should be instantiated.加载或者刷新配置文件,由于这是一个启动方法,如果失败,它应该销毁已经创建的单例,以避免悬空资源。换句话说,在调用这个方法前,要么全部实例化,要么根本不实例化。在初始化 openFeign 组件的最后,会调用 SubContext 的 refresh()操作,最终会触发 SubContext 发出ContextRefreshedEvent事件。 【为啥出现多次触发ContextRefreshedEvent **】监控类**的每个ApplicationContext都会加载两次。在 Spring 框架中,事件(Event)是会沿着 Context 层次向上传播。简单来说子 Context 发出初始化完成事件,进而引发父 Context 也发出相同事件,而父 Context 此时并没有真正初始化完成。
搜索日志发现:Bean '监控类#1' of type [******] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying);通常是由于 Spring 容器中的 Bean 在创建和初始化的过程中未能完全处理,导致该 Bean 无法被所有的 BeanPostProcessor 处理,尤其是无法进行自动代理。正是因为Feign提前触发ContextRefresh 。这也是为啥没办法通过BeanPostProcessor#postProcessAfterInitialization判断监控类#1是否被代理的原因。