# # # ready # # Targetunderstand the basic process of Spring IOC# # Related ResourcesOffical doc:http://docs.spring.io/spring/docs/4.3.9.release/spring-framework-reference/htmlsingle/Sample code:<Https://github.com/gordonklg/study>,spring ModuleSource version: Spring Framework 4.3.9# #测试代码
Gordon.study.spring.ioc.IOC01_DefaultListableBeanFactory.java
Ioc01.xml<beans ...> <bean id= "message" class= "Gordon.study.spring.common.Message" ><property name= "Body" value= "Hello world!"/></bean></beans># # # Analysis # # # Overall Flow analysis Org.springframework.core.io.Resource is an interface that Spring abstracts to represent the underlying resources from which the input stream (InputStream) can be obtained. Line 16th of the code creates a Org.springframework.core.io.ClassPathResource instance that represents a file resource relative to the classpath. org.springframework.beans.factory.support.DefaultListableBeanFactory is at the heart of this analysis. Defaultlistablebeanfactory implements theorg.springframework.beans.factory.BeanFactoryinterface, the Beanfactory interface defines a method for accessing the Spring bean container, through which we can get a preconfigured bean. beanfactoryThe interface defines the following methods:beanfactoryin the interfaceThe most important way is toGetbean (String), the specified instance is obtained through the bean name. The 20th line of code is throughGetbeanMethod gets an instance of the Message class from the Bean container. To be able to get a bean instance from Beanfactory, the Beanfactory implementation class should first be able to obtain the bean definition, such as the message bean configured in this example through an XML file, which requires a data model for the bean definition, in the Sprin G,org.springframework.beans.factory.config.BeanDefinitionThe interface is used to represent the bean definition. All bean definitions should be placed in the same place for management,Org.springframework.beans.factory.support.BeanDefinitionRegistryEquivalent to the Beandefinition registry, providing the Spring IOC with abeandefinition registration, get operation. Spring IOC Framework Design decision allows beanfactory implementation classes to assume beandefinitionregistry functions simultaneouslySoDefaultlistablebeanfactory implements theBeandefinitionregistryinterface, whilecontains a map<string, beandefinition> beandefinitionmap property asBeandefinition's registry. with the storageBeandefinition's registry,We also need the tool class to read out the bean definition from the configuration file,the 18th line of org.springframework.beans.factory.xml. The xmlbeandefinitionreader is used to read out the bean definition from an XML-formatted configuration file and register the bean definition with the specifiedin the Beandefinitionregistry instance (in this case,defaultlistablebeanfactoryinstance). Line 19th calls the loadbeandefinitions method to read the bean definition from the specified Resource. # # Beanfactory.getbean ProcessAnalysisWhen the program executes to line 20th to get the instance from the Bean container, you can discoverdefaultlistablebeanfactoryhas been successfully read into thebeandefinitionInformation: list<string> beandefinitionnames -[message] map<string, beandefinition> beandefinitionmap -{message=generic bean:class [gordon.study.spring.common.Message]; scope=; abstract=false; lazyinit=false; autowiremode=0; dependencycheck=0; Autowirecandidate=true; Primary=false; Factorybeanname=null; Factorymethodname=null; Initmethodname=null; Destroymethodname=null; Defined in class path resource [Ioc/ioc01.xml]} Line 20th Getbean The core process in this example is as follows:
- Attempt to get a bean instance named message from singletonobjects
Because the bean is the Singleton type by default, Defaultlistablebeanfactory contains the property map<string, object> singletonobjects is used to cache all S An instance of the Ingleton type. An instance is created only if the Singleton instance of the specified name has not been created, otherwise the instance created is returned directly.
- Marks the bean as created (or is about to be created) state. is to place the bean name in the set<string> alreadycreated .
- Generating rootbeandefinition,rootbeandefinition from Beandefinition can be seen as a merged final bean definition. In this example, rootbeandefinition is basically consistent with beandefinition, except that the scope with the null original value is changed to Singleton. - Root Bean:class [gordon.study.spring.common.Message]; scope=Singleton; abstract=false; Lazyinit=false; autowiremode=0; dependencycheck=0; Autowirecandidate=true; Primary=false; Factorybeanname=null; Factorymethodname=null; Initmethodname=null; Destroymethodname=null; Defined in class path resource [Ioc/ioc01.xml]
- will be Rootbeandefinition put to map<string, rootbeandefinition> mergedbeandefinitions .
- flows through a very complex process, based on The information in Rootbeandefinition, creates a bean instance through reflection, assembles the attributes, and then puts the bean instance into map<string, object> singletonobjects .
Spring IOC Source Simple Analysis 01-beanfactory