Spring loading process

In this article, we take a look at the Spring loading process.

Basic concept

1.bean

Bean: Simply put, Spring creates and manages entity objects

2.beanDefinition

BeanDefinition is the definition of the bean. Kind of like a “Class” for Spring objects.

3.BeanFactory,ApplicationContext

The BeanFactory is a basic container. ApplicationContext is a powerful subinterface to the BeanFactory and is a high-level container.

4.BeanFactoryPostProcessor ,BeanDefinitionRegistryPostProcessor

Both are post processors. Is an important extension point for Spring. Both of these extension points are triggered after the BeanDefinition is loaded, but before the Bean is instantiated. Official advice is BeanDefinitionRegistryPostProcessor to add BeanDefinition, spring BeanFactoryPostProcessor to modify BeanDefinition. BeanDefinitionRegistryPostProcessor takes precedence over spring BeanFactoryPostProcessor implementation.

5.BeanPostProcessor

The BeanPostProcessor is the post-processor for beans and an important extension point for Spring. It is triggered when the Bean is initialized. PostProcessBeforeInitialization initialization pretreatment, postProcessAfterInitialization initializes the post-processing.

6.InitializingBean

The InitializingBean implements the afterPropertiesSet method and is called when the bean is initialized

7.BeanFactory ,FactoryBean

Why put these two together, because they look alike, other than like, nothing else matters. BeanFactory is the base container, and FactoryBean is a special bean, a FactoryBean. Also an important extension of Spring, create a real column object through the getObject() method of this class. Many plugins actually use this, for example, MyBatis Mapper is an interface, no concrete implementation but can operate the database, in fact FactoryBean to it produces a proxy class.

Basic loading process

public ConfigurableApplicationContext run(String... args) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); ConfigurableApplicationContext context = null; Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>(); configureHeadlessProperty(); / / the first step: get and start the listener SpringApplicationRunListeners listeners = getRunListeners (args); listeners.starting(); try { ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); // Step 2: According to SpringApplicationRunListeners and parameters to prepare environment ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments); configureIgnoreBeanInfo(environment); // prepare the Banner printer Banner printedBanner = printBanner(Environment); // Create the Spring Container Context = CreateApplicationContext (); exceptionReporters = getSpringFactoriesInstances( SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context); / / step 4: the Spring container pre-processing prepareContext (the context, the environment, listeners, applicationArguments, printedBanner); // Refresh the container "refreshContext"; // Spring container afterRefresh(Context, ApplicationArguments); // Listener.started (context); Listener.started (context); // Runners this. CallRunners (context, applicationArguments); stopWatch.stop(); // return context; } catch (Throwable ex) { handleRunFailure(context, listeners, exceptionReporters, ex); throw new IllegalStateException(ex); }}

CreateApplicationContext () creates the container here, adding some specific post-handlers. Including the behind us say ConfigurationClassPostProcessor this post processor, also it is the most important in the spring loaded post processor. The refreshContext(context) method is the one we need to focus on, which is the core method that transfers our object to the Spring container.

public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh"); // Prepare this context for refreshing. prepareRefresh(); // Tell the subclass to refresh the internal bean factory. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context. prepareBeanFactory(beanFactory); try { // Allows post-processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory); StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process"); / * * * to activate a variety of the BeanFactory processor, including BeanDefinitionRegistryBeanFactoryPostProcessor and ordinary spring BeanFactoryPostProcessor * To perform the corresponding postProcessBeanDefinitionRegistry method and postProcessBeanFactory * / invokeBeanFactoryPostProcessors (the beanFactory); BeanPostProcessor (BeanFactoryPostProcessor, BeanFactoryPostProcessor, BeanFactoryPostProcessor, BeanFactoryPostProcessor) Will be in bean instantiation of corresponding method * / registerBeanPostProcessors (the beanFactory); beanPostProcess.end(); // Internationalize initMessageSource(); / / initialization event and listening initApplicationEventMulticaster (); // Expansion point onRefresh(); // Listeners are registerListeners(); / / initialize the bean finishBeanFactoryInitialization (the beanFactory); // Release container initialization completion message finishRefresh(); } catch (BeansException ex) { if (logger.isWarnEnabled()) { logger.warn("Exception encountered during context initialization - " + "cancelling refresh attempt: " + ex); } // Destroy already created singletons to avoid dangling resources. destroyBeans(); // Reset 'active' flag. cancelRefresh(ex); // Propagate exception to caller. throw ex; } finally { // Reset common introspection caches in Spring's core, since we // might not ever need metadata for singleton beans anymore... resetCommonCaches(); contextRefresh.end(); }}

InvokeBeanFactoryPostProcessors (the beanFactory), this method will activate BeanDefinitionRegistryPostProcessor post processor, Some of them are ConfigurationClassPostProcessor this class, it will be eligible bean into beanDefinition. FinishBeanFactoryInitialization (the beanFactory), the method will be according to BeanDefinition to initialize the beans, after completion of this step is taken over by our bean by spring.