我如何从ApplicationContext中删除一个Singleton Spring bean? [英] How can i remove a singleton spring bean from ApplicationContext?
问题描述
我想开发一个模块控制系统,以便每个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 ?
推荐答案
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屋!