Last time, I talked about the architecture and outline of Spring. Spring IOC is the most core and original framework, which is actually the original of Spring. Spring is relying on IOC to slowly develop into other Data, Web and AOP. I remember when I used Spring in the project, I knew to configure XML, reference the bean configured by Spring in Struts2, reference this class, you can use it directly, do not directly new, completely regardless of the principle, it is convenient but need to configure 2 times, once is spring bean.xml. Once is when the Struts reference is configured. This time I’ll focus on how spring IOC is implemented at the bottom.

Spring IOC

Composition and architecture of the IOC container

  • What is the IOC

IoC is not a technology, just an idea, an important object-oriented programming discipline that can guide us in designing loose-coupled, better programs. In traditional applications, we actively create dependent objects within classes, which leads to high coupling between classes and is difficult to test. With the IoC container, the control of creating and finding dependent objects is given to the container, which injects composite objects. Therefore, objects are loosely coupled between objects, which facilitates testing, functional reuse, and more importantly, makes the overall architecture of the program very flexible. Basically, managing beans (bean creation, bean storage, bean retrieval).

  • BeanFactory

The BeanFactory interface is the most basic IOC container interface, which specifies the most basic functions for the implementation of a specific IOC container.

BeanFactory class architecture

BeanFactory source code analysis

public interface BeanFactory {

    // This is a FactoryBean escape definition, because if you retrieve a FactoryBean by its name, the object you get is a factory-generated object,
    // If you want to get the factory itself, add "&"
    String FACTORY_BEAN_PREFIX = "&";

    // Get the bean instance in the IOC container based on its name
    Object getBean(String name) throws BeansException;

    // Get the bean instance based on the bean name and Class type, if the bean instance based on the name has a different Class type than the required
    // An exception is thrown
    <T> T getBean(String name, Class<T> requiredType) throws BeansException;

    // Get the beans managed in the IoC container based on the specified type
    <T> T getBean(Class<T> requiredType) throws BeansException;

    // Overrides the getBean(String name) method, whose mutable arguments are used to specify the parameters required by the constructor or factory method
    // Overrides previously defined parameters in the bean
    Object getBean(String name, Object... args) throws BeansException;

    // Overrides the getBean(Class
      
        requiredType) method, whose mutable parameters are used to specify the parameters required by the constructor or factory method
      
    // Overrides previously defined parameters in the bean
    <T> T getBean(Class<T> requiredType, Object... args) throws BeansException;

    // Determine if there is a bean with this name in the IOC container
    boolean containsBean(String name);

    // Query whether the Bean with the specified name is a singleton
    boolean isSingleton(String name) throws NoSuchBeanDefinitionException;

    // Query whether the Bean with the specified name is of prototype type
    boolean isPrototype(String name) throws NoSuchBeanDefinitionException;

    // Query whether the class type of the Bean with the specified name supports a particular class type
    boolean isTypeMatch(String name, Class
        targetType) throws NoSuchBeanDefinitionException;

    // Query the class type of the Bean with the specified nameClass<? > getType(String name)throws NoSuchBeanDefinitionException;

    // Query all aliases for the Bean with the specified name
    String[] getAliases(String name);

}
Copy the code

BeanFactroy Instantiation of a bean

The realization of the spring Ioc container, from the source is the beanfactory, but really can as a can be used independently of the Ioc container or DefaultListableBeanFactory, so you can say so,

DefaultListableBeanFactory is the ancestor of the spring ioc, this research through its former life in our understanding of the importance of the concept of the spring ioc. The creation of Spring beans is a typical factory pattern. This series of Bean factories, also known as IOC containers, provide a lot of convenience and basic services for developers to manage dependencies between objects. There are many IMPLEMENTATIONS of IOC containers in Spring for users to choose and use, and their relationships are as follows:

  • Management Bean
  1. The creation of a Bean
  2. The Bean’s store
  3. The Bean’s access
  4. Automatic detection and injection of dependencies
  5. Automatic Bean generation

What constitutes a Beandefinition

Foxconn’s iPhone, all the research and development is done in the United States. That is to say, Foxconn factory itself does not define our iPhone, it is defined by the RESEARCH and development of the US iPhone, and then Foxconn to produce, store and transport the phone. The iPhone is like our bean.

  • Definition of Bean properties
  1. name
  2. id
  3. Scope (single case, multiple cases)
  4. className
  5. parent
  6. lazyInt
  7. depends
  8. property
  • Bean definition store
  1. xml
  2. properties

BeandefinitionReader loads the parse Bean

Spring provides a bean Definition parser in two ways: PropertiesBeanDefinitionReader and XmLBeanDefinitionReader properties file format of the bean definition XML parser and file format of the bean definition parser.

  1. Load the file
  2. Into the Document
  3. Resolve the registered Beandefinition

process

  • Building an IOC container falls into the following six steps
  1. Define the Spring configuration file.
  2. The Spring configuration file is abstracted from the Resource object into a Resource object.

Define the Bean factory. 3. Define the XmlBeanDefinitionReader object and pass in the factory as an argument for subsequent callbacks. 4. Read previously abstracted Resource objects (containing the parsing of XML files) using the XmlBeanDefinitionReader object. In essence, the XML file parsing is by XmlBeanDefinitionReader by 5. BeanDefinitionParserDelegate entrusted to complete (used to entrust mode) 6. The IoC container is created, the user can get to the desired object of information through the container.

  • Simplified version process
  1. Load BeanDefinition into BeanFactroy
  2. Go to getBeanDefinition when you get the bean
  3. Then the createBean

The flow chart

The entire architecture of the IOC container

PS: It is really troublesome to read the source code, but the source code of Spring is about 120 MB, equivalent to a JAVA JDK, read our daily development is really useful, design mode: singleton mode, factory mode, builder mode.