preface

This article is the first introduction to interpreting the source code of the Spring IoC module. Introduce the concepts and design of Spring IoC.

What is IoC

Inversion of Control, or IoC, is a design principle used in object-oriented programming to reduce coupling between computer code. One of the most common is called Dependency Injection, or DI, and another is called Dependency Lookup. With inversion of control, when an object is created, it is passed (injected) a reference to the object on which it depends by an external entity that regulates all objects in the system.

— from Wikipedia

In large applications, multiple classes are required to work together to implement business logic. This makes each object need to get a reference to the object it works with at work.

If this fetching process is done on its own, the code becomes highly coupled and difficult to test. This is very detrimental to the design of complex OOP systems.

In OOP systems, objects encapsulate data and data processing actions, and the dependency of objects is reflected in the dependence of data and methods. These dependencies can be accomplished by handing dependency injection of objects to the framework or IoC container.

In a nutshell:

  • Control: the control of the current object over its internal member objects/the process of obtaining assembly objects
  • Reverse: this process/control is managed by a dedicated third party component (container or platform)

This surrendering of control from concrete objects increases the testability of the code while decoupling it. The benefits are as follows:

  1. You don’t have to build it. You just use it.
  2. Enjoy the benefits of singletons, high efficiency and no wasted space.
  3. Easy to unit test and switch mock components.
  4. Easy to AOP and transparent to consumers.
  5. Unified configuration for easy modification.

Spring IoC

In Spring, the IoC container is the vehicle for implementing this pattern, either injecting data directly into objects at generation or initialization, or injecting dependency on method calls by injecting object references into object data fields. This dependency injection can be recursive, with objects being injected layer by layer.

In this regard, this scheme has a complete and concise aesthetic feeling, it establishes the object dependency relations orderly, simplifies the management of object dependency relations, simplifies the complexity of object-oriented system to a large extent.

Spring IoC provides a basic JavaBean container that manages dependencies through the IoC pattern and enhances the basic capabilities of transaction management, lifecycle management, and other POJO objects such as Javabeans through dependency injection and AOP facets.

IoC container design

In the design of the Spring IOC container, we can see two main families of containers (by name),

  • The simple container series that implements the BeanFactory interface only implements the most basic functions of the container.
  • ApplicationContext, an advanced form of container, adds many frame-oriented features and ADAPTS to the application environment;

To the user, these are containers, different representations of containers, and what containers are used depends entirely on the user’s needs.

BeanFactory

The BeanFactory interface defines the most basic form of the IoC container and provides the most basic service contract that the IoC container should adhere to. It is also the lowest level and the most basic programming specification that we should adhere to using the IoC container. These interface definitions outline the basic IoC.

The entire inheritance tree down is pretty complex, and you don’t need to know everything about it, just like we mentioned before how to learn the source code and find the core classes. Now let’s pick a few key BeanFactories and talk about them. Avoid confusion in the source code analysis section.

  • The ListableBeanFactory interface allows you to retrieve multiple beans from the ListableBeanFactory interface. If you look at the source code, you will find that the top-level BeanFactory interface methods are used to retrieve individual beans.
  • The ApplicationContext inherits HierarchicalBeanFactory, and the Hierarchical word itself speaks for itself, meaning that we can have multiple BeanFactories in our application, You can then set the individual BeanFactories to parent-child relationships.
  • AutowireCapableBeanFactory the name of the Autowire everyone are very familiar with, it is used for automatic assembly Bean to use, but look at above, ApplicationContext didn’t inherit it, but don’t worry, don’t use inheritance, Do not represent can not use a combination, if you see ApplicationContext last method in the interface definition of getAutowireCapableBeanFactory ().
  • DefaultListableBeanFactory, in Spring, is actually the DefaultListableBeanFactory as a default function complete the IoC container to use. Contains the important functions of a basic IoC container.

I recommend that you read their JavaDoc to see how the authors describe the interface. More accurate.

ApplicationContext

ApplicationContext is a high-level IoC container, ApplicationContext used the basis of the BeanFactory MessageSource, ApplicationEventPublisher, ResourcePatternResolver these interfaces, These interfaces provide the following new features for ApplicationContext that the BeanFactory does not:

  • Support different information sources. We see that ApplicationContext extends the MessageSource interface. The extended capabilities of these information sources can support the implementation of internationalization and provide services for developing multilingual versions of applications.
  • Access resources. This feature is reflected in support for ResourceLoader and Resource, so that we can get Bean definition resources from different places. This abstraction gives users the flexibility to define Bean definition information, especially from different I/O paths.
  • Support for application events. Inherited interface ApplicationEventPublisher, thus mechanism are introduced in the context of events. The combination of these events and the Bean life cycle facilitates Bean management.
  • Additional services provided in ApplicationContext. These services enrich the functionality of the basic IoC container. Because of these rich additions, ApplicationContext is used in a frame-oriented style compared to a simple BeanFactory, so it is generally recommended to use ApplicationContext as the basic form of the IoC container when developing applications.

BeanDefinition

On the basis of these spring-provided interface definitions and implementations of the basic IoC container, Spring manages various objects in spring-based applications and their interdependencies by defining BeanDefinitions.

BeanDefinition holds our Bean information, such as which class the Bean points to, whether it is a singleton, whether it is lazily loaded, which beans the Bean depends on, and so on.

BeanDefinition abstracts our definition of beans and is the primary data type that makes containers work. For IoC containers, BeanDefinition is the data abstraction of the object dependencies managed in the dependency inversion mode, as well as the core data structure of the container to realize the dependency inversion function. The dependency inversion function is completed around the processing of this BeanDefinition.

These Beandefinitions are like water in a container, and it’s the basic data that makes the container work.

conclusion

In the following sections we will begin to look at the actual implementation of Spring IoC, container initialization and dependency injection. Most of it is source code interpretation.

For those of you who haven’t read source code before, why do you read source code? How to read the source code?

reference

  • Inside Spring Technology
  • Javadoop.com/post/spring…