I am reading Pro Spring 2.5 and I have a question to chapter 2 where you make a simple bean factory. They talk about a “bean registery”, is this the “Map” that holds the list it reads from the property file (could be whatever format I assume)?
So if this is correct the BeanFactory does essentially this:
- Create bean factory and read a configuration file
- Put the content of the configuration file in a registery (Map, List of objects whatever)
- Client invoke getBean method with a bean name and then the factory uses the registery to find it and instantiate it.
Is this correct?
They talk about extracting the BeanRegistery to an interface and then the BeanFactory implements this interface but I guess the principles are the same? It is only done for good coding purposes?
I believe you’re referring to BeanDefinitionRegistry. It simply acts as a way to keep track of BeanDefinition objects (ie. what you’ve put in your xml file, assuming you’re using xml).
Implementations do not have to do any instantiation. That is left to the BeanFactory implementation. Typically this is the way things happen:
In practice, the BeanFactory and BeanDefinitionRegistry are the same class, and the BeanFactoryImplementation will instantiate the appropriate BeanDefinitionReader.
Take the org.springframework.context.support.ClassPathXmlApplicationContext implements both BeanDefinitionRegistry and BeanFactory, and will internally construct an XmlBeanDefinitionReader, and controls the interaction between the components.