This is the 21st day of my participation in the August Text Challenge.More challenges in August
preface
Spring source code too much, from IOC, AOP, Spring life cycle and Spring extension point, these core functions, to understand the clever and essence of the Spring framework design, this series is estimated to be long, if there is an understanding of the wrong place also welcome big men to point out and discuss.
1. Basic concepts
1.1 What is the Spring IOC Container?
Inversion of Control (IoC of Control), which transfers the right to call the object which is controlled by the program code directly to the container, and implements the assembly and management of object components through the container. The concept of “inversion of control” is the transfer of control over a component object from the program code itself to an external container.
Spring IOC is responsible for creating objects, managing them (through dependency injection (DI), assembling them, configuring them, and managing their entire life cycle.
1.2 Main functions of IOC
Spring’s IoC design supports the following features:
- Dependency injection
- Depend on the check
- Automatically.
- Support the collection
- Specify initialization and destruction methods
- Support for callbacks to some methods (but need to implement the Spring interface, slightly intrusive)
The most important of these is dependency injection, which, in XML configuration terms, is the REF tag. Corresponds to the Spring RuntimeBeanReference object.
For the IoC, the most important thing is the container. The container manages the Bean life cycle and controls the dependency injection of beans.
1.3 Implementation principle of Spring IoC
The implementation principle of IoC in Spring is factory mode plus reflection mechanism.
1.4 What is the difference between BeanFactory and ApplicationContext?
BeanFactory and ApplicationContext are two core Spring interfaces that can be used as Spring containers. ApplicationContext is the subinterface of the BeanFactory.
1.4.1 Dependencies
BeanFactory: Is the lowest level interface in Spring. It contains the definition of various beans, reads Bean configuration documents, manages Bean loading and instantiation, controls Bean life cycle, and maintains the dependency relationship between beans.
The ApplicationContext interface, a derivative of the BeanFactory interface, provides more complete framework functionality in addition to the functionality of the BeanFactory:
- Inheriting MessageSource, thus enabling internationalization.
- Unified access to resource files.
- Provides events to register beans in listeners.
- Load multiple configuration files simultaneously.
- Load multiple (inheritable) contexts so that each one focuses on a specific layer, such as the Web layer of the application.
1.4.2 Loading Mode
BeanFactroy uses lazy loading to inject beans, meaning that a Bean is loaded and instantiated only when it is used (by calling getBean()). This way, we won’t be able to find some existing Spring configuration issues. If a property of the Bean is not injected, the BeanFacotry load does not throw an exception until the first use of the getBean method.
ApplicationContext, which creates all the beans at once when the container is started. This way, we can find configuration errors in Spring when the container starts, which makes it easier to check whether the dependent properties are injected. By preloading singleton beans after the ApplicationContext starts, you ensure that you don’t have to wait for them to be created when you need them.
The only disadvantage of the ApplicationContext relative to the basic BeanFactory is that it takes up memory. When an application has many configured beans, the application starts slowly.
1.4.3 Creation Method
BeanFactory is usually created programmatically, and ApplicationContext can also be created declaratively, such as using ContextLoader.
1.4.4 Registration method
BeanFactory and ApplicationContext both support the use of BeanPostProcessor and BeanFactoryPostProcessor, but the difference between them is: BeanFactory needs to be registered manually, whereas ApplicationContext is automatically registered.
1.5 ApplicationContext Container implementation class
FileSystemXmlApplicationContextTo load the definition of beans from an XML file, the full path name of the XML Bean configuration file must be supplied to its constructor.
ClassPathXmlApplicationContext: from an XML file to load the beans, the definition of here, you need to properly set the classpath because the container will be in the classpath to find bean configuration.
WebXmlApplicationContext: Loads an XML file that defines all the beans of a WEB application.
1.6 What is Spring’s dependency injection?
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 (DI**) and another is called Dependency Lookup.