解决 Spring Cloud 中 context has been closed already

context has been closed already 解决方案

报错代码

有这样一段代码,在运行中可能会发生 context has been closed already 错误,且一旦发生,以后每次运行到这必定出错。

@Component
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext context = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextExt.context = applicationContext;
    }

	/**
	 * 可以通过该静态方法从 Spring 上下文中获取想要的 Bean。
	 */
	public static <T> T getBean(Class<T> cls) {
        if (context == null)
            throw new IllegalStateException("no application context aviliable.");
        try {
            return (T) context.getBean(cls); // 这里报错!!!
        } catch (BeansException e) {
			throw new RuntimeException(e);
        }
        return (T) null;
    }
}    

原因分析

但是类路径中包含 spring-cloud-context.jar 时,因为 ContextRefresher 这个类会在上下文刷新时关闭旧的上下文,导致这个静态类里的上下文是已经关闭的,因此会产生这个错误。

触发探索

省略debug过程,直接说原因,发现架构组设计了个钩子,当收到中央服务发送的【插件安装广播】时,会通过 refresh Spring 上下文以重新加载配置,以达到运行时安装插件的效果。

以下是代码示例:

public class MsgConcumer {

    private static ApplicationContext context = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextExt.context = applicationContext;
    }

	/**
	 * 坑爹的二方包,自己设计了个钩子,当收到中央服务发送的【插件安装广播】时,会通过 refresh Spring 上下文以重新加载配置。
	 */
	public void onMessage(Message msg) {
		if("reload_config".equlas(msg.getType()) {
			context.refresh();
		}
	}
}    

解决

  1. 修改设计方案,手动写代码加载,而不是这样偷工减料。
  2. 【不推荐】对使用jar包的人提要求,如果使用了该jar包,所有 setApplicationContext() 的地方都要再去监听 ContextedRefresh 通知重新设置 context。
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值