Spring's IOC container implementation

    Introduction to IOCs

    Inversion of Control IOC (Inversion of Control) is a design idea, DI (Dependency Injection) is a way to implement IoC, and some people think that DI is just another way of saying IoC. In programs without IoC, we use object-oriented programming. The creation of objects and the dependencies between objects are completely hard-coded in the program. The creation of objects is controlled by the program itself. After control is reversed, the creation of objects is transferred to a third party (container), The so-called inversion of control is: the way to obtain dependent objects is reversed.

    The core of the idea of ​​IOC is that resources are not managed by the two parties who use the resources, but by a third party who does not use the resources, which can bring many benefits. First, centralized management of resources to achieve configurable and easy management of resources. Second, it reduces the degree of dependence on both parties using resources, which is what we call coupling.

    In the design of Spring IOC, there are two main container series (below): one is a simple container that implements the BeanFactory interface, which only implements the most basic functions; the other is the ApplicationContext application context, which is an advanced form of the container, adding framework-oriented characteristic.

Spring manages various objects in Spring-based applications and the dependencies between them     by defining BeanDefinitions . The IOC container is used to manage the object dependencies. For the IOC container, the BeanDefinition is the data abstraction (core data structure) for managing the object relationship in the dependency inversion mode. BeanDefinition is like water in a container. With basic data, the container can function.

BeanFactory

    The BeanFactory interface defines the most basic form of the IOC container, and provides the lowest-level and most basic programming specifications that need to be complied with when using the IOC container.

BeanFactory与FactoryBean

    BeanFactory is a Factory, which is an IOC container or object factory. The main method is getBean(String beanName), which returns a Bean with a specific name from the container, but one of them is FacotryBean.

    FactoryBean is a special kind of bean, which is actually a factory. The Bean created by FacotryBean is obtained through FactoryBeanName, and the Bean created through getObject(). To get the FactoryBean itself, it must be obtained through &FactoryBeanName, that is, the FactoryBean is obtained through getBean(&FactoryBeanName) in the BeanFactory.

Note: In spring, BeanFactoryUtils.isFactoryDereference() is used to determine whether a Bean is a FactoryBean.

In the internal implementation of spring, when the Bean is obtained through the getBean(String beanName) of BeanFacotry, if the Bean is a FactoryBean, the Bean it generates will be returned, otherwise, the Bean will be returned directly.

Implementation principle of BeanFactory container

 Here takes XMLBeanFactory as an example, the initialization process:

1. Create the abstract resources of the IOC configuration file, including the definition information of the BeanDefinition.

2. Create a BeanFactory, where DefaultListableBeanFactory is used.

3. Create a reader that loads the BeanDefinition. Here, XMLBeanDefinitionReader is used to load the BeanDefinition in the form of an XML file, and configure it to the BeanFactory through a callback.

4. Load configuration information from the defined resource location, and the specific parsing process is completed by XMLBeanDefinitionReader. After completing the entire loading and registration bean definition, the required IOC container is resumed. At this time, the IOC container can be used.

ApplicationContext

    ApplicationContext is an IOC container with advanced morphological meaning, which provides the following features:

    1. Support different information sources. Extend the MessageResource interface, for example to support internationalized implementations.

    2. Access resources. For ResourceLoader and Resource support, Bean definition resources can be obtained from different places.

    3. Support application events. It inherits the interface ApplicationEventPublisher and introduces the event mechanism in the context.

    4. Provide additional services in the ApplicationContext. Make it a frame-oriented usage style.

BeanDefinition registration

    After the BeanDefinition registration is completed, the initialization process of the IOC container is completed. At this time, the configuration information of the entire Bean has been established, and the BeanDefinition can be used, which is retrieved and used in the beanDefinitionMap. The role of the container is to process and maintain this information.

IOC container dependency injection

    The process of dependency injection is triggered when the user asks the IOC container for a bean (getBean) for the first time, or the container can complete the pre-instantiation of the bean by controlling the lazy-init attribute in the BeanDefinition.

    In the process of Bean creation and object dependency injection, it is necessary to recursively complete dependency injection according to the information in the BeanDefinition. Recursion all use getBean as the entry. One recursion is a recursive call to find the required bean in the context and create a bean; the other recursion is to recursively call the container order getBean method to obtain the dependent bean of the current bean, and at the same time start the creation and injection of the dependent bean. .

    After the bean creation and object dependency injection are completed, a series of beans linked by dependencies are established in the IOC container. Through the relevant interface methods of the IOC container, they can be easily used by upper-layer applications.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325304140&siteId=291194637