ApplicationContext refresh process and resolve a number of important processor

review

Above fact, we have implemented a simple BeanFactoryit has the function

  • Registration Bean container to obtain Bean by qualified name
  • You can intercept the process before and after the initialization Bean
  • And is about to be destroyed to do some logic after injecting property Bean
  • Solve the circular dependency

In fact, sum up the method to achieve it is: the definition of load Bean, Bean is instantiated, it is very simple

Bean in the spring in full life cycle, you can view the spring of their own BeanFactoryinterface, which is described in detail in the top notes.

But the actual application scenarios in addition to the main Bean management, there are some news broadcasts, international, event listeners, etc.

In this article

Think about this article analyzes ApplicationContextthe refreshprocess, first to a summary of the process, in order to follow a bunch of boring source code analysis

  • Initial phases: preparation

  • First, we get a first and BeanFactory, initialized here isDefaultListableBeanFactory

  • The second step, we need to configure this BeanFactoryspel expression parser like, Environmentmust be configured to inside the factory, so that subsequent processing

  • The third step, we allowed to add BeanFactoryProcessorto modify BeanFactorythe Bean, Common subclass BeanDefinitionRegistryPostProcessorit may be added to the container defined Bean

    Like @ Import, @ PropertySource, @ ComponentScan , @ ImportResource, @ Bean methods are also tk.mybatis is sufficient to inject custom of Bean defined , and clear definition of what is bean read my last article

  • The fifth step, of course, is a callback to collect all of the above to BeanFactoryProcessorthe

  • The sixth step, the callback all collected BeanPostProcessor, these processorare already added to the container, if you see here, do not know what is BeanPostProcessor, please look back above the bean life cycle callback is not clear that would not work, you not enough experience to see this article

  • The seventh step, international initialization (to the body, this is considered a branch line, because in most cases we are not written in international projects)

  • The eighth step, initialization event broadcaster. Event: common, such as the page loads have onLoad event, the application has just started onLaunch event, with the project not by much, much more likely to use it is an internal spring

  • Ninth step, to leave a callback to initialize a particular subclass of the bean, the default empty

  • The tenth step, registering listeners (all achieved ApplicationListener class), and to look at some of the events prior to release

  • A tenth step, substantially single instantiation embodiment, substantially all classes of our project in this method instantiated

  • Closure: cleanup

See the benefits of this process

  • IOC container initialization familiar with the process, we can better use the spring
  • Some tools like spel nature of things can take over direct use, no need to repeat cited third-party libraries or create the wheel

text

Not every step of the fine chemicals, only some of the key steps to interpret, like how to load the definition of Bean, read xml and read notes, the reader can read on their own, this article discusses several key processor

We all know, Bean's life cycle, can be added BeanPostProcessorbefore the bean initialization and after doing some initialization process, in the same BeanFactory have a BeanFactoryPostProcessorpermit you BeanFactory after initialization, modify BeanFactory.

Example:

We look at the PropertyPlaceholderConfigurerproduct of the times xml configuration, its successor is structured as follows

PropertyResourceConfigurer implements BeanFactoryPostProcessor
   |-PlaceholderConfigurerSupport
      |-PropertyPlaceholderConfigurer

We usually configure this in the beginning

<context:property-placeholder location="classpath:jdbc.properties" />

PropertyPlaceholderConfigurerIs used to project @Valuevalues are processed, injected into the attribute value, look how it implements the following

View the key method PropertyResourceConfigurer.postProcessBeanFactory, is divided into three steps

Properties mergedProps = mergeProperties();     // 从 location 中加载属性

// Convert the merged properties, if necessary.
convertProperties(mergedProps);                 //什么都没干,一个模板方法,可用于密码加密,自定义属性转换

// Let the subclass process the properties.
processProperties(beanFactory, mergedProps);    // 真正处理属性的地方,解析 spel 表达式 ${}

The real process of using the Visitor pattern in the BeanDefinitionVisitormiddle.

springboot is how to deal with it, springboot is a fixed configuration file application.properties, instead of using the location to configure the path it uses PropertySourcesPlaceholderConfigurerto parse the configuration, it also inherited from PlaceholderConfigurerSupportand rewrite postProcessBeanFactory key methods used in the final surface the

PropertySourcesPropertyResolver Properties were resolved.

You can use this article to see

BeanDefinitionRegistryPostProcessorInherited from BeanFactoryPostProcessorits argument is BeanDefinitionRegistrya Bean definition of the registrar, what it can do about it, see its methods can be defined to do CRUD bean, powerful right.

registerBeanDefinition
removeBeanDefinition
getBeanDefinition
containsBeanDefinition
getBeanDefinitionNames
getBeanDefinitionCount
isBeanNameInUse

Sample one:

In the ConfigurationClassPostProcessormiddle, it implements BeanDefinitionRegistryPostProcessorit uses ConfigurationClassParserto parse the project configuration @Configurationclass, and then use ConfigurationClassBeanDefinitionReaderthe class loader to resolve the Bean defined

View the details of this article

Sample II:

In the tkmybatis MapperScannerConfigurerare also realized BeanDefinitionRegistryPostProcessor, which use ClassPathMapperScannerinto the container to scan Mapper classes, add

public class ClassPathMapperScanner extends ClassPathBeanDefinitionScanner

Plan focuses

In conclusion, in fact, refresh process just to give you a rough framework for implementation, specific treatment or, in some processor, listener, the online source interpret most of all to take this refresh process bigger space to explain, in fact, not much necessary, like the servlet life cycle, like, understand their execution, but the rest of the process is to go with the current sub-class of.

Share an article is right

On BeanPostProcessor and BeanFactoryPostProcessor in spring and a callback initialization

Little promotion

Writing is not easy, I hope the support of open source software, and my gadgets, welcome to gitee point star, fork, put bug.

Excel common import and export, support Excel formulas
blog address: https://blog.csdn.net/sanri1993/article/details/100601578
gitee: https://gitee.com/sanri/sanri-excel-poi

Use the template code, the gadget generate code from the database, and some projects can often be used in the
blog address: https://blog.csdn.net/sanri1993/article/details/98664034
gitee: https://gitee.com/ sanri / sanri-tools-maven

Guess you like

Origin www.cnblogs.com/sanri1993/p/11829442.html