Spring Core: Implementation of IoC Container

There are many ways to rely on Inversion of Control. In Spring, the IoC container is the carrier that implements this pattern. It can directly inject data into the object when the object is generated or initialized, or it can inject the dependency on the method call by injecting the object reference into the object data field. . This dependency injection is recursive, and objects are injected layer by layer. In this regard, this scheme has a complete and concise beauty, it establishes the dependencies of objects in an orderly manner, simplifies the management of object dependencies, and simplifies the complexity of object-oriented systems to a large extent.


Design and implementation of loC container series: BeanFactory and ApplicationContext

In the design of the Spring IoC container, we can see two main container series, one is the simple container series that implements the BeanFactory interface, and these series of containers only implement the most basic functions of the container; the other is the ApplicationContext application context, which acts as a It exists as an advanced form of container. Based on the simple container, the application context adds many framework-oriented features and makes many adaptations to the application environment. With these two basic container series, most of the user's needs for the use of IoC containers can be basically met.

Spring's loC container series

The loC container provides many conveniences and basic services for developers to manage dependencies between objects. There are many IoC containers for developers to choose from, and SpringFramework's IoC core is one of them, which is open source. So what exactly is an IoC container? What exactly does it look like in the Spring framework? In fact, for the users of the IoC container, the BeanFactory and ApplicationContext that we often come into contact with can be regarded as the specific manifestation of the container. If we go deep into the implementation of Spring, what we usually call the IoC container actually represents a series of container products with different functions, but the functions of the container are large and small, and have their own characteristics. This is the same in Spring, Spring has a variety of
IoC container implementations for users to choose and use. What kind of container to use depends entirely on the user's needs, but it is very helpful to know the basic situation of the container before using it. From a code point of view, I can see the design of this series of containers.


Just as a product needs to have product specifications, as an IoC container, it also needs to specify a basic functional specification for its specific implementation. The design of this functional specification is expressed as an interface class BeanFactory, which reflects Spring's provision for users to use. The most basic functional specification set by the IoC container. For Spring's concrete IoC container implementation, what are the basic characteristics it needs to meet? It needs to meet the basic interface definition of BeanFactory, so you can see the status of this BeanFactory interface in the inheritance system in the figure. It appears as a most basic interface class in Spring's IoC container system.
On the basis of the interface definition and implementation of the basic loC container provided by these Spring, Spring manages various objects in Spring-based applications and their interdependencies by defining BeanDefinition. BeanDefinition abstracts our definition of Bean and is the main data type for the container to work. We all know that in the computer world, all functions are based on the abstraction of reality through data. The IoC container is used to manage object dependencies. For the IoC container, BeanDefinition is the data abstraction of the object dependencies managed in the dependency inversion mode, and it is also the core data structure for the container to implement the dependency inversion function. It is all done around the processing of this BeanDefinition. These BeanDefinitions are like water in a container. With these basic data, the container can function. In the following analysis, BeanDefinition will appear many times.
At the same time, when using the IoC container, it is also important to understand the difference between BeanFactory and ApplicationContext for our understanding and use of the IoC container. Knowing the difference and connection between these two important containers means that if we have the ability to distinguish different container products in the container series. Another advantage is that if you need to customize the container implementation of a specific function, you can easily find a suitable product in the container series as a reference without redesigning.

Guess you like

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