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?
By using the @Autowired annotation and naming the field with the bean name.
Enabling @Autowired annotationBy declaring beans, you provide metadata to the Spring Container to return the required dependency object at runtime. This is called Spring Bean Autowiring. In java based configuration, all the bean methods are defined in the class with @configuration annotation.
The most commonly used simple BeanFactory implementation is org. springframework.
If annotation-config mode is enabled then this should work
class Bean
@Autowired
BeanFactory factory;
...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With