One, control reversals (inversion of control, abbreviation IOC) and Dependency injection (Dependency injection, abbreviation di)
1, control reversal
IoC (inversion of Control), which is The core of spring, runs through all the Times. The so-called IoC, for the Spring Framework, is the spring that is responsible for controlling the life cycle of objects and the closing between objects. System.
Previously, we created objects like this:
/*//Normal call Ipersondao persondao=new Persondao (); Persondao. Save (); /* ///Factory mode call Ipersondao Persondao = Dataaccess.createpersondao (); Persondao. Save (); */
First of all, the method of direct new, which exposes the creation of objects, and then, after we realize this, we may encapsulate the creation of this object as a method to invoke, thus masking the object's creation details, which is of great benefit for creating complex objects.
However, both of these methods are created by the object and, of course, after it is created, it is up to us to manually maintain its life cycle and its direct reference relationship with other classes. If there is such a way, I can avoid the creation of complex objects, so that I am immune to the various parameters of the problem, and for me to maintain the life cycle of the object and its relationship in the system; All I have to do is show up when I need this class and I don't have to worry about it when I don't need it.
So, the container of this kind of thing appeared, spring is just a thing to realize the container idea.
At this time, object creation, destruction, processing various reference relations are given to spring to handle. All the classes will beSpringregister in the container and tellSpringthis is a Something , you need something, and thenSpringwill give you what you want when the system runs to the right time, and also give the object to other things that need it. All classes are created and destroyed by theSpringcontrol, which means that the object's life cycle is no longer the object that references it, butSpring. For a specific object, it was previously controlled by other objects, and now it is all objects have beenSpringcontrol, so this is called control inversion.
2, Dependency injection
IoCone of the key points of the system is to dynamically provide an object with other necessarylike. This is done byDI(Dependency Injection, Dependency injection) to achieve. such as objectsAneed to operate the database, before we always have toAWrite your own code in order to get aConnectionobject, with theSpringall we have to do is tellSpring,Aneed aConnection, as for thisConnectionHow to construct, when to construct,Adon't need to know. In the system at run time,Springat the right time to create aConnection, and then, like a needle, injected intoA, this completes the control of the relationship between the various objects. Aneed to rely onConnectionto work properly, and thisConnectionis bySpringinjected intoA, the name of the dependency injection comes in this way. --from Baidu
Actually think about the injection principle, no matter what. NET or Java, is reflection. Reflection from the surface is based on a character to create something, this thing can be an assembly, class, property, Method ... The benefit of reflection is dynamic, in the complex operating environment, I do not know the system to run to a certain moment need something, this time, we can use reflection to achieve dynamic creation process, compared with the flexibility of reflection, we can almost ignore the performance problems it brings.
Two, spring.net implementation of object creation
First Look at our demo structure:
to use sping in the console, add a reference to spring:
Interface class:
Namespace demoone{Public interface Ipersondao {//<summary>// Saves this instance. </summary>/ /<remarks> Creator: Liu Huishuo; creation time: 2015-09-12 20:45:25</remarks> void Save ();} }
Implementation class:
namespace demoone{public class Persondao:ipersondao {//<summary>// Saves this instance. </summary>/ /<remarks> created by: Liu Huishuo; Created: 2015-09-12 20:45:20</remarks> public void Save () { Console.WriteLine ("La la la la la ... Save it. "); } }}
Next, simply say the two common configuration files +xml How to configure spring:
1. Create an XML file that holds the information for the classes that spring will manage, such as Objects.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><objects ><description></description><object id= " Persondao "type=" Demoone.persondao,demoone "/> <!--Configure the class to be generated--></objects>
then the application configuration file:
<?xml version= "1.0" encoding= "utf-8"?><configuration><!--Configure processing class for processing nodes--><configsections> <sectiongroup name= "Spring" ><section name= "context" type= "Spring.Context.Support.ContextHandler, Spring.core "/><section name =" Objects "type=" Spring.context.support.defaultsectionhandler,spring.core "/> </sectionGroup></configSections><!--Spring Configuration--><spring><!--configuration: Use an external XML file as an object configuration, Note: The external XML to be copied to the build path--><context><resource uri= "File://objects.xml" ></resource></context> </spring></configuration>
Simple calling Code:
Use of spring iapplicationcontext IAC = Contextregistry.getcontext (); Ipersondao DAO = IAC. GetObject ("Persondao") as Ipersondao; if (dao!=null) { dao. Save (); Console.WriteLine ("Spring Test Success"); Console.readkey (); }
Of course, we can also write the configuration information of the class in the config, without using the external XML:
<?xml version= "1.0" encoding= "utf-8"?><configuration><!--Configure processing class for processing nodes--><configsections> <sectiongroup name= "Spring" ><section name= "context" type= "Spring.Context.Support.ContextHandler, Spring.core "/><section name =" Objects "type=" Spring.context.support.defaultsectionhandler,spring.core "/> </sectionGroup></configSections><!--Spring Configuration--><spring><!-- Configuration mode Two: Save all configurations in the application configuration file--><context><resource uri= "Config://spring/objects" ></resource>< /context><objects><object id= "Persondao" type= "Demoone.persondao,demoone"/> <!--Configure the class to be generated-- </objects></spring></configuration>
Alternatively, you can either load the XML directly by using a programmatic method, or configure spring with a mixed XML and Config method.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
spring.net--understanding control inversion and dependency inversion