问题描述
我想向同一BeanFactory
创建的Bean注入Spring BeanFactory
有什么办法吗?
I want to inject a Spring BeanFactory
to a Bean created by the same BeanFactory
is the any way to do so?
顺便说一句,我正在开发一个Web应用程序.如果不是,我知道我可以通过拥有RequestContext
来获得BeanFactory,但是我要注入BeanFactory
的bean不在requestContext
中,但仍在应用程序上下文中.我能做到吗?
by the way, I'm developing a web application.If not I know that I can get the BeanFactory by having RequestContext
but the bean I want to inject the BeanFactory
is not in the requestContext
but still in the application context. can I do that?
推荐答案
您的bean可以实现 BeanFactoryAware
.通过实现此接口,您的bean将通过调用此方法来接收BeanFactory
:
Your bean can implement BeanFactoryAware
. By implementing this interface, your bean will receive the BeanFactory
through a call to this method:
void setBeanFactory(BeanFactory beanFactory) throws BeansException
顺便说一下,有一个类似的界面(ApplicationContextAware
),以便在需要时检索ApplicationContext
.
By the way, there is a similar interface (ApplicationContextAware
) in order to retrieve the ApplicationContext
if you need to.
这篇关于将BeanFactory注入Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!