Reputation: 1094
I want to inject a Spring BeanFactory
to a Bean created by the same BeanFactory
is the any way to do so?
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?
Upvotes: 10
Views: 10371
Reputation: 135992
If annotation-config mode is enabled then this should work
class Bean
@Autowired
BeanFactory factory;
...
Upvotes: 18
Reputation: 11717
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
By the way, there is a similar interface (ApplicationContextAware
) in order to retrieve the ApplicationContext
if you need to.
Upvotes: 14