发生原因:
为了方便获取ApplicationContext,定义ApplicationContextUtil类存放applicationContext。
nacos server更改配置,触发RefreshEvent事件,RefreshEventListener接受到事件调用LegacyContextRefresher.refresh(),方法内部会调用updateEnvironment,然后有一段代码将老的applicationContext给关闭了,代码如下:
finally {
ConfigurableApplicationContext closeable = capture;
while (closeable != null) {
try {
closeable.close();
}
catch (Exception e) {
// Ignore;
}
if (closeable.getParent() instanceof ConfigurableApplicationContext) {
closeable = (ConfigurableApplicationContext) closeable.getParent();
}
else {
break;
}
}
}
导致其他地方调用ApplicationContextUtil.applicationContext.getBean()方法时,会报错context has been closed already。
解决办法:
定义ApplicationListener方法监听EnvironmentChangeEvent事件,然后将新的context替换老的context。
public class ApplicationContextRefreshListener implements ApplicationListener<Applicati
7794



被折叠的 条评论
为什么被折叠?



