LuckyLuke
LuckyLuke

Reputation: 49097

BeanFactory implementation

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:

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?

Upvotes: 0

Views: 1780

Answers (2)

Matt
Matt

Reputation: 11815

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:

  1. BeanFactory typically constructs a BeanDefinitionReader
  2. BeanDefinitionReader will get bean definitions (typically from xml) and register with BeanDefinitionRegistry
  3. BeanFactory will ask the BeanDefinitionRegistry for a BeanDefinition, and then instantiate it.

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.

Upvotes: 2

duffymo
duffymo

Reputation: 309008

No, you don't have to do any of that. It's much easier than you're thinking.

Just get an ApplicationContext and you're good to go. It reads the config, creates and wires the beans, and makes them available by name.

ClassPathXmlApplicationContext is a good choice for the concrete implementation if you're not writing a web app. You can load all your configuration from a directory in the classpath that way.

Upvotes: 0

Related Questions