BeanDefinition: BeanDefinition: BeanDefinition: BeanDefinition: BeanDefinition: BeanDefinition: BeanDefinition: BeanDefinition When were the beanprocessors initialized? And when did we make the @Configuration annotation?

context

ApplicationContext

We mentioned earlier that the Spring Framework is decoupled and that bean parsing is defined and created separately. Remember how we wrote our first Spring Framework demo without SpringBoot:

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        Object bean = ctx.getBean("testBean");
    }
Copy the code

You can seeClassPathXmlApplicationContextOne constructor takes a spring configuration file as an argument, and then fetches the bean object via the getBean method. The least we can tell from the above two lines:ClassPathXmlApplicationContextThe bean configuration can be read from the configuration file and then initialized for all beans. And it has the upper interfaceApplicationContextAlso inheritedBeanFactoryInterface (getBean). So let’s take a lookApplicationContextClass diagram:

As you can see, ApplicationContext inherits a large number of interfaces in addition to BeanFactory. It actually provides a composite function. ApplicationContext combines all application-level operations and provides them in a unified manner:

  • BeanFactoryOperation interface to the bean container
  • EnvironmentCapableYou can get the Environment object
  • ApplicationEventPublisherEvent publishing can be provided
  • ResourcePatternResolverYou can load resources from the classpath or file system through rule matching
  • MessageSourceProvide an international interface

It a child of the interface has two: ConfigurableApplicationContext and WebApplicationContext, corresponding to the two main application class, one kind is to provide standard application/configuration, operation is a web application (for SpringMVC) provide operations/configuration. Ultimately, the difference between the different implementation classes corresponds to the implementation difference of getting beans from different configuration sources.

Context is usually referred to as a Context object. It is usually a limited set of shared data through which we can manipulate shared data during data transfer. The same is true of the ApplicationContext, which is essentially a shared object for the entire application, through which we can retrieve/manipulate (partially) shared data across the entire application hierarchy.

Initialize the

We are here to ClassPathXmlApplicationContext as an example to illustrate the core process of ApplicationContext. ClassPathXmlApplicationContext all constructors will eventually go ApplicationContext implementation class of one of the most core method refresh

  1. Before refresh, we set the configuration file we passed in

  1. Then enter the Refresh method, which consists of the abstract superclassAbstractApplicationContextImplementation, which defines the overall refresh flow and steps

Several key steps are discussed here

It is obtainFreshBeanFactory, this method is used to get the actual the BeanFactory, finally returned to the BeanFactory DefaultListableBeanFactory, namely we mentioned on the part of, The final implementation classes for all BeanFactories. There is also a more important abstract method, provided by AbstractRefreshableApplicationContext loadBeanDefinitions method, Different implementation classes implement their own BeanDefinition fetch methods and register them with the BeanFactory.

There is another important step is invokeBeanFactoryPostProcessors, at this time, the BeanFactory has been ready, Here will call all spring BeanFactoryPostProcessor and BeanDefinitionRegistryPostProcessor implementation, the two rear processor is actually two extension points are given, which gives us the opportunity to can modify the BeanFactory, The latter give us the opportunity to modify the BeanDefinition, and they have two important implementations:

  • ConfigurationClassPostProcessorParse all @Configuration Configuration classes
  • PropertyResourceConfigurerReplace the placeholder

Finally is the finishBeanFactoryInitialization, not finished all singleton object initialization of lazy loading. Iterate over all the beanName and call the getBean method.

The Refresh method defines the initialization process for the entire ApplicatonContext, and subclasses are responsible for implementing the specific process of some of the steps. Does not look familiar, the application here is the standard template method design pattern.

conclusion

Finally, we summarize the simple process for initializing a bean:

  1. Load the beanDefinition, which can be created through a file, annotations, or manuallyBeanDefinitnioN and load toBeanFactory
  2. Can be achieved byBeanFactoryPostProcessorModify the beanFactory orBeanDefinitionRegistryPostProcessorModify beanDefinition
  3. throughgetBeanMethod to initialize all beans
  4. Put all initialized beans into cache (Map)