This article is to learn and organize reference
IOC Overview:
in Spring , theIoC container implements dependency control inversion, which either injects data directly into an object when it is generated or initialized, or injects the dependency of a method call by injecting an object reference into the Object data field. This dependency injection can be recursive, and the object is injected layered. The interdependence between objects is managed by the IoC container and injected by the container, such as object creation, assignment, and so on, which greatly simplifies development.
The structure diagram is as follows
1.1 Beanfactory
Interface Beanfactory: represents the Most basic functional specification that spring sets for the Io C container provided to the user.
Interface Beandefinition:spring manages the various objects in the spring container and their interdependencies through beandefinition.
The source code is as follows
** *the most basic functions of the IOC container*This class is equivalent to a specific container product in a container*@author QJC*/ Public InterfaceBeanfactory {/*** Gets the Factorybean of the resulting object * such as: MyObject is a factorybean, using &myobject to get the Factorybean*/String Factory_bean_prefix= "&"; /*** Get the beans managed in the IOC container *@paramname *@return * @throwsbeansexception*/Object Getbean (String name)throwsbeansexception; /*** Determine if the container contains a bean of the specified name *@paramname *@return */ BooleanContainsbean (String name); /*** Check if the bean with the specified name is a singleton (can be specified in beandefinition) *@paramname *@return * @throwsnosuchbeandefinitionexception*/ BooleanIssingleton (String name)throwsnosuchbeandefinitionexception; /*** Check if the bean with the specified name is a prototype type (can be specified in beandefinition) *@paramname *@return * @throwsnosuchbeandefinitionexception*/ BooleanIsprototype (String name)throwsnosuchbeandefinitionexception; /*** Check if the class type of the bean with the specified name is a specific class type *@paramname *@paramtargetType User designation *@return * @throwsnosuchbeandefinitionexception*/ BooleanIstypematch (String name, Class targetType)throwsnosuchbeandefinitionexception; /*** Query the class type of the specified name bean *@paramname *@return * @throwsnosuchbeandefinitionexception*/Class<?> GetType (String name)throwsnosuchbeandefinitionexception; /*** Query All aliases of the specified name bean (user specified in beandefinition) *@paramname *@return */string[] getaliases (String name);}
Description
Only the basic behavior of the IOC container is defined in beanfactory, and it doesn't care how your bean defines how it is loaded. Just as we care about what the factory is getting, the basic interface does not care as to how the factory produces the objects.
1.2 IoC container xmlbeanfactory
This class is the lowest-level implementation class for the IOC container, providing basic IOC functionality. Similar to the ApplicationContext implementation principle, the IOC container function is acquired by holding or extending the defaultlistablebeanfactory.
The relationship structure diagram is as follows
Source
Public classXmlbeanfactoryextendsDefaultlistablebeanfactory {/*** Xmlbeandefinitionreader: Read the XML class*/ Private FinalXmlbeandefinitionreader reader =NewXmlbeandefinitionreader ( This); /** * @paramresource spring is used to encapsulate the classes of IO operations, which can be constructed as follows * Classpathresource resource = new Classpathresource ("Bean.xml"); * @throwsbeansexception*/ PublicXmlbeanfactory (Resource Resource)throwsbeansexception { This(Resource,NULL); } PublicXmlbeanfactory (Resource Resource, Beanfactory parentbeanfactory)throwsbeansexception {Super(parentbeanfactory); This. Reader.loadbeandefinitions (Resource); }}
Basic steps for IOC container creation
New Classpathresource ("Bean.xml"); New defaultlistablebeanfactory (); New Xmlbeandefinitionreader (factory); Reader.loadbeandefinitions (Resource);
1. Create an abstract resource for the IOC configuration file (contains benadefinition definition information)
2. Create Beanfactory
3. Create Beandefinition Reader
4, read the configuration information, completed loading and registration after the IOC container was created
Usually we do not create this, but instead use the Applictioncontext interface to create
1.3 Applictioncontext
Applictioncontext belongs to the Advanced IOC container, which adds features on the beanfactory basis (other containers do not).
1. Support information sources, can achieve internationalization. (Implement Messagesource interface)
2. Access resources. (Implement the Resourcepatternresolver interface, this is the next to speak)
3. Support App events. (Implement Applicationeventpublisher interface)
Reference:
The Insider of Spring technology
Implementation of the "in-depth spring" IOC container