将BeanFactory注入Bean [英] Injecting BeanFactory into a Bean

查看:121
本文介绍了将BeanFactory注入Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向同一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可以实现

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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