Self-understanding and implementation process and life cycle of Spring IOC

Contents
  1. IOC Overview
  2. Design of
  IOC Container 3. Definition and Initialization of Spring IOC Container
  4. Beand Lifecycle
  5. IOC Summary

IOC overview

IOC (Inversion of Control): is also a container for publishing beans

Inversion of control is a very abstract thing, then you have to understand who controls who? Who was reversed?

Control: In traditional Java SE programming, we create objects directly inside the object through new, and the program actively creates dependent objects; while IoC has a special container to create these objects, that is, the Ioc container controls the creation of objects; who Control who? Of course the IoC container controls the object; what? That is, it mainly controls the acquisition of external resources (not just objects including files, etc.)

Inversion: In traditional applications, we actively control the object to directly obtain dependent objects, that is, forward rotation; while inversion is the container to help create and inject dependent objects.

Why is it reversed instead of forward? Because the container helps us find and inject dependent objects, the object only passively accepts dependent objects, so it is reversed. The acquisition of the object is reversed.

For example: you are developing and responsible for the commodity trading module, but the transaction involves the financial module, and you do not understand the financial module, so you cannot easily deal with it. At this time, what you expect is:

  • Familiar with financial processes
  • The interface logic is as simple as possible, just need to call
  • The interface instance can be obtained through a simple description, and the description should be as simple as possible

The actual example and the development idea (IOC) are also very similar: the
  financial interface object is not created by itself, but created by the financial department, but it also meets your needs. In the subconscious, you think that your object should be created by you, but in fact it is not what you really need, because maybe you are not proficient in the business you need. This is the concept of Inversion of Control.

The concept of inversion of control: Inversion of control is a way to generate or obtain a specific object through a description (which can be XML or annotation in java) and through a third party.

In the Spring instance, the inversion of control is implemented by the IOC container, which is implemented by dependency injection (DI). Spring will provide an IOC container to manage the corresponding resources. For example, the development of the financial interface module has been completed. It is only necessary to publish the financial module to the IOC container, and then publish the transaction module to the IOC container. Use XML or annotations to configure the dependency injection relationship between the interfaces, and Spring will manage it. And the test only needs to get the content of the financial module in the IOC container.

This is an idea of ​​Inversion of Control. Its biggest idea is to reduce the coupling between objects. For some classes in the system, it is not necessary to understand how to implement it, just what it is useful for. That is, the generation of objects depends on the IOC container, rather than the active behavior of the developer. The pattern of actively creating objects, the responsibility rests with the developer. In the passive creation mode, the responsibility belongs to the IOC container. In this passive mode, we can understand that the object is inversion of control.

IOC container design

The design of the Spring IOC container is mainly based on the two interfaces of BeanFactory and ApplicationContext. The main interface of the IOC container is shown in the figure:
write picture description here
From this figure, it can be seen that the BeanFactory is the bottom layer of the design, and the ApplicationContext is the high-level interface.

The main methods of the BeanFactory class are some getBean methods. The parameters can be extended interfaces or parent classes. However, when there are multiple implementation classes (classes with multiple subclasses) under the interface, the parent class cannot obtain accurate information. , because the container cannot determine the concrete implementation class.
There is also the isSingleton method: used to determine whether it is a singleton. If true, the bean exists as the only instance in the IOC container.
The isPrototype inverse method is the opposite of the isSingleton method. When the judgment is true, it means that when you get the Bean from the container, the container will generate a new strength for you.
By default: Spring will create a singleton for the bean, that is, by default isSingleton returns true and isPrototype returns false.

Definition and initialization of Spring IOC container

Bean definition:

  1. Resoure positioning:
    The IOC container performs resource positioning according to the developer's configuration
  2. BeanDefinition loading:
    At this time, the information of the resource file located by the Resource is saved to the Bean Definition (BeanDefinition), and the Bean instance will not be created at this time.
  3. BeanDefinition registration:
    Publish the loaded information to the IOC container. Note that Bean instances will not be created at this time.

Bean initialization:
Spring Bean has a configuration option - lazy-init, the default value is default, the actual value is false, that is, the IOC container will automatically initialize the bean by default. If it is set to true, the initialization of the bean will only be performed when we use the getBean method of the IOC container to obtain it.

Bean's life cycle

  1. definition
  2. initialization
  3. dependency injection
  4. If setBeanName
    implements the setBeanName method of the interface BeanNameAware interface, then it will call this method
  5. setBeanFactory
    If the bean implements the setBeanFactory method of the BeanFactoryWare interface, it will call this method
  6. setApplicationContext
    If the bean implements the setApplicationContext method of the ApplicationContext interface, and the IOC container must also be an implementation class of the ApplicationContext interface, this method will be called, otherwise it will not be called.
  7. postProcessBeforeInitialization
    If the bean implements the postProcessBeforeInitialization method of the BeanPostProcess interface, it will call this method
  8. afterPropertiesSet
    If the bean implements the afterPropertiesSet method of the BeanFactoryPostProcess interface, it will call this method
  9. Custom initialization method
    If you customize the initialization method, call the defined initialization method
  10. postProcessAfterInitialization
    If the Bean implements the postProcessAfterInitialization method of the BeanPostProcess interface and completes these calls, the Bean has completed the initialization at this time, then the Bean lives in the IOC container, and the user can obtain the Bean things from it.
  11. lifetime
  12. destroy
    If the Bean implements the destroy method of the DisposableBean interface, it will call this method
  13. Custom destroy method
    If you customize the destroy method, call the custom method

IOC Summary

  For IOC inversion of control to understand the actual instance of the interface, it will be better understood. Inversion of control is just a thought pattern, but it is more abstract and difficult to understand. It is clearer to realize abstract things.

  The IOC container serves to manage beans.

  Pay attention to the basic methods defined by BeanFactory, because BeanFactory is the most basic underlying design of IOC

  Bean life cycle, pay attention to which life cycle interfaces and methods need to be implemented, it allows us to customize initialization and destruction.

Guess you like

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