“This is the 26th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.
The IOC and DI
IOC (Inversion of Control) concept
Usually in the development of the program, each object in the use of its cooperative object to take the initiative to create this cooperative object, when needed to take the initiative to create, the right to create in their own hands, but this way coupling degree is very high; After using the Spring, the right to object creation to the Spring container to handle, the so-called control object is to create the control to the container to create, or container to control the object to create, initially created the object create actively, is forward, and reverse is to rely on object creation and injection for Spirng container handling, Objects passively accept dependent objects.
Injection pattern
In IOC, there are three ways to provide dependent objects for injected objects: constructor injection, setter injection, and interface injection
DI(dependency injection) concept
A container dynamically injects dependencies into a component, actively injecting its dependent classes into an object when the container instantiates it
IOC architecture
Design structure drawing
The following is a brief analysis of the structure diagram to understand the interface design diagram
The first interface design thread runs from BeanFactory to HierarchicalBeanFactory to ConfigurableBeanFactory, which defines basic methods such as getBean(), HierarchicalBeanFactory adds the getParentBeanFactory interface to make the BeanFactory manage the parent IOC container; ConfigurableBeanFactory ConfigurableBeanFactory ConfigurableBeanFactory ConfigurableBeanFactory ConfigurableBeanFactory ConfigurableBeanFactory ConfigurableBeanFactory ConfigurableBeanFactory ConfigurableBeanFactory ConfigurableBeanFactory ConfigurableBeanFactory ConfigurableBeanFactory ConfigurableBeanFactory ConfigurableBeanFactory ConfigurableBeanFactory ConfigurableBeanFactory ConfigurableBeanFactory Through the superposition of these interface designs, the BeanFactory is defined as the basic function of a simple IOC container
The second design thread is the interface design of the ApplicationContext, From the BeanFactory to ListableBeanFactory ApplicationContext, to our common WebApplicationContext or ConfigurableApplicationContext. L Two interfaces in this interface architecture, ListableBeanFactory and HierarchicalBeanFactory, connect the BeanFactory interface definition to the ApplicationContext interface definition. In the ListableBeanFactory interface, many BeanFactory interface functions are refined, such as the getBeanDefinitionNames() method; For ApplicationContext interface, it through inheritance MessageSource, ResourceLoader, ApplicationEventPublisher, The BeanFactory simple IOC container adds a lot of feature support for advanced containers.
The Resource system
Org. Springframework. Core. IO. Resource, its each implementation class represents a week resources access strategy, for example
ClassPathResource, RLResource, FileSystemResource, etc.
ResourceLoader system
When you have resources, load them. Spring uses ResourceLoader to load unified resources
The BeanFactory system
The BeanFactory provides the most basic IOC container functions and provides the most basic service contracts that IOC containers should comply with. The BeanFactory only one interface, and such as DefaultListableBeanFactory, XmlBeanFactory, ApplicationContet can provide the concrete implementation.
BeanFactory basic method
-
The containsBean (String Name) method determines whether there is another Bean with that name
-
The isSingleton (String Name) method can determine if it is a singleton Bean by name
-
The isPrototype(String Name) method can determine if it is a multi-instance Bean by name
-
isTypeMatch(String name, Class<? > typeToMatch) methods can depend on whether the Class type of the named Bean is a specific Class type
-
The getType(String name) method queries the Class type of the Bean with the specified name
-
GetAliases (String name) Queries the aliases of all beans with the specified name
Design principles of the BeanFactory container
The BeanFactory interface provides the basic interface specification for using the IOC container. Spring also provides a series of containers that conform to the IOC container for implementation. Take the implementation of XmlBeanFactory as an example:
You can see the connections between the classes in the figure
From the source, the XmlBeanFactory inherited from DefaultListableBeanFactory, XmlBeanFactory added reads the XML file
This method is used to read XML from the XmlBeanFactory, pass Resource as an argument, initialize the xmlBeanDefinitionReader object, and use this object to complete the loadBeanDefinitions call
BeanDefinition system
Used to describe Bean objects in Spring.
BeanDefinitionReader system
Read the contents of the Spring configuration file and turn it into a data structure inside the IOC container :BeanDefinition
ApplicationContext system
In addition to providing the basic functions of containers described earlier, the ApplicationContext provides users with new features that the BeanFactory does not have
-
Support different information sources. ApplicationContext extends the MessageSource interface, and the extended capabilities of these information sources can support the implementation of internationalization
-
Access resources: We can use Resource to get bean-defined resources in different places
-
Support application events: inherited interface ApplicationEventPublisher and mechanism are introduced in the context of events
ApplicationContext container design principles
We through FileSystemXmlApplicationContext implementation as an example to illustrate the design principle of ApplicationContext
FileSystemXmlApplicationContext has two important functions
- A function is applied directly to use FileSystemXmlApplicationContext, support for instantiate the application context, at the same time start the IOC container refresh () process. Can see this in FileSystemApplicationContext code implementation:
This refresh () process involves the complex operation of starting the IOC container, and details of how reresh() behaves when the IOC container is started will be examined later
- Another feature has to do with how XML’s bean-definition resources are loaded from the file system.
The implementation code is as follows:
Call this method to get the resource location of FileSystemResource