I. Spring IOC principle

1. The concept

Spring describes beans and their dependencies through a configuration file, instantiates beans and establishes dependencies between beans using the Java language’s reflection capabilities. Spring’s IoC container also provides advanced services such as Bean instance caching, lifecycle management, Bean instance proxy, event publishing, resource loading, etc.

2. High-level View of the Spring container

When Spring starts, it reads the Bean configuration information provided by the application, and generates a corresponding Bean configuration registry in the Spring container. Then, it instantiates beans according to this registry, assembs the dependencies between beans, and provides a ready running environment for the upper layer applications. The Bean cache pool is a HashMap implementation

3. IOC container implementation

BeanFactory- Framework infrastructure

BeanFactory is the infrastructure of the Spring framework, oriented towards Spring itself; ApplicationContext is intended for developers using the Spring framework, and in almost all applications we use ApplicationContext directly rather than the underlying BeanFactory.

BeanDefinitionRegistry registry

(1). Each node element in the Spring configuration file is represented in the Spring container by a BeanDefinition object, which describes the Bean configuration information. The BeanDefinitionRegistry interface provides methods for manually registering BeanDefinition objects with containers.

BeanFactory top-level interface

(2). At the top of the class structure tree, its main method is getBean(String beanName), which returns a Bean with a specific name from the container. The functionality of BeanFactory is extended by other interfaces:

ListableBeanFactory

(3). This interface defines several methods to access the basic information of beans in the container, such as checking the number of beans, obtaining the configuration name of a certain type of Bean, checking whether a certain Bean is included in the container, etc.

HierarchicalBeanFactory Parent-child cascade

(4) interface of parent-child cascading IoC container, child container can access parent container through interface method; HierarchicalBeanFactory Interface, Spring’s IoC container can establish a parent-child hierarchy of containers that can access beans in the parent, but the parent can’t access beans in the child. Spring uses parent-child containers to implement many functions, such as in Spring MVC, presentation layer beans are in a child container, while business layer and persistence layer beans are in the parent container. In this way, presentation layer beans can reference business layer and persistence layer beans, while business layer and persistence layer beans cannot see presentation layer beans.

ConfigurableBeanFactory

(5). It is an important interface to enhance the customizability of IoC containers. It defines methods to set up class loaders, property editors, container initialization post-processor, etc.

AutowireCapableBeanFactory automatic assembly

(6). Defines the method of automatic assembly of beans in containers according to certain rules (such as matching by name, matching by type, etc.);

Singleton beans are registered during the SingletonBeanRegistry run

(7). Defines methods that allow singleton beans to be registered with the container at runtime; For singleton beans, the BeanFactory caches the Bean instance, so the second time you use getBean() to fetch the Bean instance directly from the IoC container’s cache. Spring DefaultSingletonBeanRegistry class provides a used to slow the CDS instance Bean cache, it is a HashMap with the cache, Single-instance beans are stored in the HashMap with a beanName key.

Rely on the log box

(8). When initializing the BeanFactory, you must provide a logging framework for it, such as using Log4J, which provides the Log4J configuration file under the classpath, so that you can start the Spring container without errors.

ApplicationContext is for developing applications

ApplicationContext is derived from BeanFactory and provides more application-oriented functionality.

The ApplicationContext inherits HierarchicalBeanFactory and ListableBeanFactory interfaces, and on top of that extends the functionality of BeanFactory through several other interfaces:

1. The ClassPathXmlApplicationContext: from the classpath loading the configuration file by default

2. FileSystemXmlApplicationContext: from the file system to load the configuration file by default

3. ApplicationEventPublisher: let the container has released application context function of events, including container startup, close events, etc.

4. MessageSource: Provides i18N international message access for applications;

5. ResourcePatternResolver: The classes implement the ApplicationContext realized functions, similar to the PathMatchingResourcePatternResolver can be prefixed Ant style resource file path load Spring configuration file.

LifeCycle: Added to Spring 2.0, this interface provides start() and stop() methods to control asynchronous processing. When used, the interface is implemented by both the ApplicationContext and the Bean. The ApplicationContext passes the start/stop information to all the beans in the container that implement the interface. To achieve management and control JMX, task scheduling and other purposes.

7. ConfigurableApplicationContext extension in ApplicationContext, it added two main methods: Refresh () and close(), giving ApplicationContext the ability to start, refresh, and close the ApplicationContext. Call refresh() to start the application context when it is closed, call refresh() to clear the cache and reload configuration information when it is already started, and call close() to close the application context.

3.WebApplication architecture

WebApplicationContext is designed for Web applications and allows initialization by loading configuration files from a path relative to the Web root. A reference to the ServletContext can be obtained from the WebApplicationContext, and the entire Web application context object will be placed as a property into the ServletContext so that the Web application environment can access the Spring application context.