2013/13/3
In traditional java applications, the bean life cycle is very simple. The keyword new initializes the bean, and then the producer is used. Once the bean is no longer used, it enters the garbage collection stage to process the spring bean life cycle:
So how does spring parse and manage beans from xml or automatic annotations? Beanfactory is the core of ioc in spring. The most important thing is to return a bean instance based on its name or type. Provides the most basic dependency injection and bean assembly services. But we usually use ApplicationContext, which is also extended from beanfactory. Implementation class: DefaultListableBeanFactory
/** Map of bean definition objects, keyed by bean name */ private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>();
The idea is clear here. Bean Factory Initialization is actually filling in the map. As long as we fill all the beans defined in xml here, this factory can work.
So the question is: from now on, what do we need to fill the Map? That is, initialize the bean factory, or create an IOC container. First, I listed the following steps.
1. we need a File pointing to our XML File (the configuration files in this article are all XML as an example, because this is what we are most familiar with). The major point is resource location, simply put, we need some tools to locate the location of the XML file.
2. A Reader is needed to read our XML files. The major point is DOM parsing. To put it simply, all the definitions of XML files are given.
3. You need to set all the read data to Map.
These three parts are summarized as positioning, parsing, and registration. First, let's give it a try. Code:
= ClassPathResource("beans.xml"= = "numbers: " +"person"
But no one wants to get a bean in this way. Is there a simpler way to get a bean in spring? Code:
= FileSystemXmlApplicationContext("classpath:beans.xml""numbers: " +"person"
Next, let's take a look at the FileSystemXmlApplicationContext class.
Final constructor call:
FileSystemXmlApplicationContext(String[] configLocations,
Here, refresh () is the entry to the ioc container. The refresh method is located in AbstractApplicationContext. This is an abstract class and implements the general function of ApplicationContext. refresh () the method lists the initialization steps of the ioc container. The main step is step 2, obtainFreshBeanFactory, which is used to notify the subclass to refresh the internal bean factory.
="Bean factory for " + getDisplayName() + ": " +
The template mode is used here to provide a uniform template for future child classes to be implemented. refreshBeanFactory and getBeanFactory are all abstract methods.
Let's take a look at the specific implementation of refreshBeanFactory ().
refreshBeanFactory() = (.beanFactory = ApplicationContextException("I/O error parsing bean definition source for " +
The final keyword is added to the method, which means that this method cannot be rewritten. It is clear that the initialization of IOC container occurs in this method. The first step is to determine whether there is an existing factory, if yes, it will be destroyed. Otherwise, a default bean factory will be created, that is, the DefaultListableBeanFactory mentioned above. Check loadBeanDefinitions (beanFactory). Here, after we create a default bean factory, we load the bean definition. In the FileSystemXmlApplicationContext initialization method, we have implemented the beanfactory implementation class for us.