10

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?

2 Answers 2

18

If annotation-config mode is enabled then this should work

class Bean @Autowired BeanFactory factory; ... 
Sign up to request clarification or add additional context in comments.

Comments

14

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.