Spring- initialization container frame series -SpringIOC

1. What is the IOC / DI

  IOC (Inversionof Control) Inversion of Control: The so-called inversion of control, is to create objects inside the original code we need to implement, dependent code, reverse to the container to help achieve. So inevitable we need to create a container, and a description of the need to let the container know the relationship between the object and the object needs to be created. This document describes the most concrete manifestation of our configurable.

  DI (Dependency Injection) DI: refers to the object class is passively rely instead on their own initiative to find, in other words, it means the object is not dependent on the class to find from the container, but when the container containing the active object instantiation it depends on the class to inject it.

  Who controls who and what control: traditional Java SE programming, we directly create objects through new inside the object, a program initiative to create dependent objects; and IoC is a special container to create these objects, that is controlled by the Ioc container creating an object; who controls? Of course IoC container control object; what control? That is the main control of external funding

Source Gets (not just objects include such files, etc.).

  Why is reversed, what reversed: there have reverse forward, legacy applications by our own initiative in an object to acquire control directly dependent objects, that is, forward; it is to be reversed by the container help create and inject dependent objects; why is reversed? Because the container to help us find and inject the dependent objects, the object of accepting it passively dependent objects, it is

  Reverse; what reversed? The acquisition is dependent on the object reversed.

In summary: IOC divided into two parts.

Initializing a first portion IOC container, access to external resource files (Bean management class definition data)

The second part of the IOC container Bean managed dependency injection

2.Spring core concepts

1. BeanFactory (IOC container-managed Bean)

The BeanFactory, ending Factory's, indicating that it is a factory class (interface), a facility for managing the Bean. In Spring, the core of the interface is the BeanFactory IOC containers, Its responsibilities include: instantiating or configuration application objects and the dependencies between those objects.

 

 

 

The final category is the default implementation DefaultListableBeanFactory, he implements all interfaces; BeanFactory provides the core management bean method

2. BeanDefinition

SpringIOC container management we define a variety of Bean object of their mutual relations, Bean implementation is subject to BeanDefinition described in the Spring.

Bean BeanDefinition defines a data structure used to store Bean.

 

 

 

 

Bean resolution process is very complex functions are points is very small, because there needs to be extended in many places, must ensure that there is sufficient flexibility to respond to possible changes. Bean resolution mainly to resolve the Spring configuration file. The analytical process mainly by the following figures complete class: 

3.ApplicationContext

     ApplicationContext achieved topmost BeanFactory interface, also an IOC container, Spring ApplicationContext as the start of the entrance, the entrance is initialized IOC container

    ApplicationContext system

 

 

 

 

3.IOC container initialization

Resource Initialization IOC container comprises BeanDefinition the positioning, loading and registering of these three basic processes

1.super (parent) method sets the resource loader Spring

 private final ResourceLoader resourceLoader;

2.setConfigLocations () stored in the configuration file path array (a plurality of parameters can be passed paths)

private String[] configLocations;

3.refresh () to reconstruct all over Bean

4.obtainFreshBeanFactory () loaded Bean defined resource file (from the beginning of this method)

5.refreshBeanFactory () using the delegation design pattern, the parent class defines the abstract refreshBeanFactory () method, the specific implementation calls subclass container refreshBeanFactory () method, the subclass refreshBeanFactory ()

BeanFactory determine whether there is, if there is to be destroyed beans and close beanFactory, then create DefaultListableBeanFactory, and calls loadBeanDefinitions (beanFactory) to load bean definitions.

  5.1.createBeanFactory () with createBeanFactory of beanFactory factory initialize new DefaultListableBeanFactory (getInternalParentBeanFactory ()) indicates that all are created by Bean DefaultBeanFactory ().   

DefaultBeanFactory class structure in FIG.

6.loadBeanDefinitions (beanFactory) loading method call Bean definition, here again mainly uses a delegation mode, the current class only defines the abstract methods loadBeanDefinitions specific implementation calls subclass container

        6.1.new XmlBeanDefinitionReader (beanFactory) Create Bean reader, and to set the callback to the vessel, the vessel using the reader to read the resource defined Bean

7.loadBeanDefinitions (beanDefinitionReader) passed a new method of reader Bean reader truly loaded

        7.1.getConfigResources (); Get Bean defined resource location is saved in the parameter 2 String [] configLocations;

8.reader.loadBeanDefinitions (configLocations); XmlBean reader to call the parent class read AbstractBeanDefinitionReader resources located Bean definitions

9.loadBeanDefinitions (location) is an overloaded method

10.loadBeanDefinitions (location, null) (overloads) access to resources provided in the loader initialization process ResourceLoader IoC container provided in a 1; if the interpretation is NULL, and if resourceLoader instanceof ResourcePatternResolver

11.loadBeanDefinitions (resources) to delegate design pattern delegated to implement XmlBeanDefinitionReader

12.doLoadBeanDefinitions () (overloads) reads the resource into XML encoding of special form of handle loading XML files defining resource Bean method, first / resource file into an InputStream IO stream obtained from the XML parsing source InputStream

13.doLoadBeanDefinitions () (overloaded method) Bean actual load resources from a specific definition of the XML file method

14.doLoadDocument () to convert the XML file into a DOM object parsing process is implemented by documentLoader

15.registerBeanDefinitions () This is the start of a detailed process Bean definitions resolved, the resolution process will be used Spring Bean configuration rule will be parsed and converted into an internal data structure of a container according to the resource defined Bean Bean Semantic in the Spring

16.documentReader.registerBeanDefinitions inlet parsing process, the delegation model used here, BeanDefinitionDocumentReader just interfaces, specific analytical procedure has achieved complete implementation class DefaultBeanDefinitionDocumentReader

The method is to work, 17.doRegisterBeanDefinitions (root) do begin with, start parsing Bean

18.preProcessXml (root) Bean definitions before parsing, parsing custom enhanced resolution process scalability

19.parseBeanDefinitions () entered the loading process resolution process to resolve the label Bean.xml, the contents of the xml into BeanDefinition use delegate delegate mode

20.parseDefaultElement (ele, delegate) using Spring Bean rule parsing element node

21.processBeanDefinition (ele, delegate) to enter the registration link

22.BeanDefinitionReaderUtils.registerBeanDefinition () parse the definition of Bean registered to get to Spring IOC container, which is the definition of Bean registered with the IOC container entrance into links Key is registered as Bean id value BeanDefinition

23.registry.registerBeanDefinition () register to the IOC container BeanDefinition stored in ConcurrentHashMap

private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);

 The overall flow into: First, the need to load the Bean.xml store the path to the array, creating a DefaultListableBeanFactory () vessel, get the path to convert XML files in the file path to java, java parsing the file into BeanDefinition, in the storage BeanDefinition to ConcurrentHashMap in.

Guess you like

Origin www.cnblogs.com/liboware/p/12590552.html