Pay attention to the preliminary preparation before every big operation.
prepareRefresh()
Set spring startup time
Set the spring shutdown and startup flags
Get the environment object and set some attribute values, which are from the system environment and not from xml.
Set up listeners and the collection of events to be published.
ConfigurableListableBeanFactory beanFactary = obtainFreshBeanFactory(); What I created must be a new factory
Create container object:DefaultListableFactory
Load the attribute values of the xml configuration file into the current factory, the most important thing is BeanDefinition
ConfigurableListableBeanFactory beanFactary = obtainFreshBeanFactory()
Spring bean creation overview 1_Feichun Wusha's blog-CSDN blog
Recall the picture we talked about before, in fact, the beginning of everything is to get a refreshed bean container from your factory.
ListableBeanFactory and ConfigurableBeanFactory are mentioned in the BeanFactory interface. These are two important sub-interfaces. Let's look at the DefaultListableBeanFactory class diagram, and this class is what we often use.
When we look for beans, we first look for them in the child container and then in the parent container.
There is one in our dogetbean method to find its parent container.
ListableBeanFactory Let’s look at his introduction
Introduction to ConfigurableBeanFactory
There are a bunch of set methods inside, which can fill in some corresponding attribute values.
Why are class diagrams so complicated?
Scalability
After creating the factory, we start reading the configuration file
The current factory settings are ready, let’s set some parameter values
We will set some serialized id values, customized bean factory? Load the bean definition information, which is to parse the configuration file. There are many overloads, and the parameters of each reload are different because you have more than one configuration file.
This is an overloaded method, there will be different readers in it
It’s time to take the booster
prepareBeanFactory
The ignoreDependencyInterface here ignores the implementation of an interface. Ignore aware (to obtain an object in the container, as long as the aware interface of an object is implemented, then the function of this object will be available)
This point is even ignored. At this time, the initialization is not complete, so this is ignored. When we set the aware attribute, we call invokeAwareMethod to set the attributes of the aware interface. The map that gives us an overview has
Summarize
Complete the initialization of the bean factory.
postProcessBeanFactory(beanFactory)
Clicking into the code space indicates that it can be extended or implemented.
invokeBeanFactoryPostProcessors(beanFactory)
Modify bean registration information
Note that your bfpp is also a bean, which is our spring’s built-in definition.
Must be called before instantiating the singleton object. Think about why here
That is to say, complete the execution of the above picture.
At this time, there is a preliminary stage.
registerBeanPostProcessors(beanFactory)
Pay attention to the definition of the method above.
This step is equivalent to a preparation work
initMessageSource()
This thing is an international operation.
initApplicationEventMulticaster()
Initialize the broadcaster for the corresponding event
OnRefresh()
Empty method implementation, secondary development
registerListeners
Register listener
finishBeanFactoryInitialization(beanFactory)
That's it
Just look at the key steps and methods
In Spring,
finishBeanFactoryInitialization(beanFactory)
methods are used to complete the initialization process of the Bean factory. It is the last step in the startup phase of the Spring IoC container and is used to complete the instantiation and initialization of all registered beans.Specifically,
finishBeanFactoryInitialization(beanFactory)
the method usually includes the following steps:
Instantiate non-lazy-loaded singleton beans: Traverse all non-lazy-loaded singleton bean definitions, create their instances in the bean factory, and put them into the container. These beans will be instantiated immediately when the container starts.
Execute the Bean's initialization method: For each instantiated Bean, its initialization method is called. The initialization method can be defined by a method that implements
InitializingBean
the interfaceafterPropertiesSet()
, or by a custom initialization method specified in the bean definition.Register Bean PostProcessor: Apply all registered
BeanPostProcessor
implementation classes to all instantiated beans.BeanPostProcessor
Custom initialization and modification can be performed on the Bean after it is instantiated.Complete the initialization of the Bean factory: mark the Bean factory as completed initialization state. This means that all bean instantiation, initialization, and post-processing work has been completed and the container is ready for application use.
In short,
finishBeanFactoryInitialization(beanFactory)
the method is responsible for completing the instantiation and initialization process of all registered beans in the Spring IoC container, ensuring that all beans in the container are ready for use by the application.
The methods starting with do in spring are the methods of event work.