我如何从ApplicationContext中删除一个Singleton Spring bean? [英] How can i remove a singleton spring bean from ApplicationContext?

查看:680
本文介绍了我如何从ApplicationContext中删除一个Singleton Spring bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个模块控制系统,以便每个Spring bean都可以由我自己的LifeCycle Controller进行管理.

I want to develop a module control system so that every spring bean can be managed by my own LifeCycle Controller.

但是我不知道如何从ApplicationContext中删除单例spring bean.

But I can not figure out how can I remove a singleton spring bean out of ApplicationContext.

这可能是一个有趣的问题,您能帮我解决吗?

That may be an interesting problem , can you help me to resolve ?

推荐答案

您可以尝试删除bean定义.获取

You can try removing the bean definition. Get the BeanDefinitionRegistry and call removeDefinition(..)

这取决于您创建应用程序的方式,但是例如在Web应用程序中,您可以通过以下方式获取定义注册表:

It depends on the way you create your application, but for example in web application you can get the definition registry by:

BeanDefinitionRegistry factory = 
   (BeanDefinitionRegistry) applicationCtx.getAutowireCapableBeanFactory();

(bean工厂实现BeanDefinitionRegistry).

(the bean factory implements BeanDefinitionRegistry).

我不知道bean实例是否也将被删除.试试看.

I don't know if the bean instance will be removed as well. Give it a try.

这篇关于我如何从ApplicationContext中删除一个Singleton Spring bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆