In object-oriented system, the object encapsulates the data and the processing of the data, the dependency of the object is often embodied in the dependence on the data and the method. These dependencies can be done by giving the object's dependency injection to the framework or IOC container, which is very valuable to hand over the control from the specific object, which can improve the testability of the code while decoupling the code.
In spring, the IOC container is the vehicle for implementing this pattern, which can inject data directly into an object when it is generated or initialized, or it can inject dependency on a method call by injecting an object reference into the object's data domain. Setter injection and constructor injection are the main methods of injection.
Design and implementation of IOC container series: Beanfactory and ApplicationContext
1, from the interface beanfactory to hierarchicalbeanfactory, and then to configurablebeanfactory, is a major beanfactory design path. This interface definition includes the basic method of the IOC container such as Getbean () , through which the bean can be obtained in a leisurely way. The configurablebeanfactory interface mainly defines some configuration functions for beanfactory, such as setting up the parent IOC container through Setparentbeanfactory (), Configure Bean Post processing through addbeanpostprocessor, and more.
2, with ApplicationContext application context as the core interface design, the main interface design has, from Beanfactory to Listablebeanfactory, then to ApplicationContext, To our usual webapplicationcontext or Configurableapplicationcontext interface. Our common application contexts are basically configurableapplicationconntext or webapplicationcontext implementations.
The Beanfactory interface designs the Getbean method, which is the main method of using the IOC container API, which allows the beans managed in the IOC container to be obtained. If you need to type-check the bean when you get the bean, you can use a method with parameters. Object Getbean (String name,class requiredtype);
Interface Checklist:
So how does the read function of XML in spring be implemented?
When constructing xmlbeanfactory this IOC container, it is necessary to specify the source of beandefinition information, which needs to be encapsulated into the resource class in spring. The resource class is the class that spring uses to encapsulate I/O operations.
The initialization process of the IOC container
The IOC's launch includes three basic processes for beandefinition resource positioning, injection, and registration.
The first procedure is the resource positioning process . This resource positioning refers to the beandefinition resource location, which is accomplished by resourceloader through a unified resource interface.
The second process is the loading of a beandefinition . The onboarding process is to represent the user-defined bean as the data structure inside the IOC container, and the internal structure of the container is beandefinition.
The third process is the process of registering these beandefinition with the IOC container . The beandefinition is injected into a hashmap inside the IOC container, and the IOC container holds the beandefinition data through this hashmap.
In the Spring IOC design, bean-defined onboarding and dependency injection are two separate processes. Dependency injection typically occurs when the application first requests beans from the container through Getbean. With one exception, as we set the Lazyinit property to a bean, the bean's dependency injection is done in advance when the IOC container is initialized, without having to wait until the entire initialization is complete and the first time the Getbean is used.
Spring IOC Container Summary (not finished)