Home > Article > Backend Development > Analysis of Spring Core Components
Spring Core Components
Spring has many core components, but its skeleton is Core, Context and Bean.
Bean
Among the three, Bean is the core of the core. Beans implement objects through configuration files, and Spring manages object storage space and life cycle allocation. Through dependency injection, objects can be injected into specified business logic classes. These injection relationships are managed by the Ioc container.
Therefore, the core idea of Spring is often called BOP (Bean Oriented Programming), Bean-oriented programming.
Bean component is defined under Spring's org.springframework.beans package, which solves the following problems:
Definition of Bean
Creation of Bean
Parsing of Bean
Users only need to pay attention to the creation of Bean, other Both processes are completed internally by Spring.
1. Overall architecture
The overall architecture of Spring Bean is a typical factory model, and the top-level interface is BeanFactory. ListableBeanFactory, HierarchicalBeanFactory and AutowireCapableBean are its subclasses in order to distinguish the data limitations of Spring's internal object processing and conversion.
ListableBeanFactory: Indicates that these beans are listable
HierarchicalBeanFactory: Indicates that these beans have an inheritance relationship
AutowireCapableBeanFactory: Defines the automatic assembly rules of beans
These interfaces respectively define Bean collections, Bean relationships and Bean relationships. Behavior.
2.Bean definition
Bean definition is mainly described by BeanDefinition, and the hierarchical relationship is as follows:
The nodes defined in Spring’s configuration file will be converted into BeanDefinition objects after successful parsing, and all subsequent operations will be performed on Performed on BeanDefinition object.
3.Bean parsing
The main task of Bean parsing is to parse the Spring configuration file and finally generate the BeanDefinition object.
The parsing process is very complex, including all tags in the configuration file. The main participating classes are as follows:
Context
Bean wraps Objects, and Objects store the data required by the business. Therefore, how to provide a survival and operating environment for these data and the relationships between them (that is, saving the state of the object) is the problem that Context needs to solve. Context is actually a collection of Bean relationships, also called an Ioc container.
ApplicationContext is the top-level interface of Context. The hierarchical relationship is as follows:
ApplicationContext can identify the basic information of an application environment. It inherits 5 interfaces to expand the functions of Context. Among them, BeanFactory is used to create beans, and it inherits ResourceLoader interface for accessing any external resources.
Subclasses of ApplicationContext, mainly include:
ConfigurableApplicationContext: A Context in which users can dynamically configure and modify information, among which AbstractRefreshableApplicationContext is the most commonly used.
WebApplicationContext:
Context prepared for web applications, which can directly access ServletContext.
In short, the functions that ApplicationContext must complete are as follows:
Identify an application environment
Use BeanFactory to create Bean objects
Save object relationship tables
Capture various events
As an Ioc container, Context is responsible for most other functions of Spring Base.
Core
Spring is a package of tools for discovering, establishing and maintaining relationships between beans, called Core. This is actually the required Util.
One of the important components of Core is Resource.
1. Overall level of Resource
Resource mainly defines the access method of resources. All resources are abstracted into the Resource interface. The hierarchical relationship is as follows:
Resource packaging. Resource inherits the InputStreamSource interface upwards, and all resources are obtained through InputStream, thereby shielding the resource provider.
Resource loading. ResourceLoader interface under Resource, all resource loaders can load all resources by uniformly implementing this interface, such as the previous ApplicationContext.
2. Establish a relationship with Context
As shown in the figure, ApplicationContext interacts with ResourceLoader through the ResourcePatternResolver interface to load, parse and describe resources. ResourcePatternResolver encapsulates and integrates resources to facilitate use by other components.
Summary
This article mainly summarizes the three core components that constitute the Spring skeleton framework and the connections between them, as well as some insights into the understanding of the implementation principles of the three.