usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSpring.context;usingSpring.Context.Support;usingcommon.logging;namespacetest2{/// <summary> ///before learning to spring.net this control inversion (IOC) and aspect-oriented (AOP) container framework, let's take a look at what a control reversal (IOC) is. ///control inversion (inversion of the control, English abbreviation for IOC), also called Dependency injection (Dependency injection). ///I personally think that the control reversal means that the dependent objects (control) change, the original class itself to manage the dependent objects into the IOC framework to manage these objects,///makes it possible to rely on the control of the detached class itself to achieve loose coupling. /// </summary> classProgram {Static voidMain (string[] args) { //Common ImplementationIpersondao DAO =NewPersondao (); Dao. Save (); Console.WriteLine ("I'm the General method ."); //Program must know the Ipersondao interface and the Persondao class. In order not to expose specific implementations,//I can use the abstract Factory in the design pattern to solve the problem. Ipersondao daofactory =Dataaccess.createpersondao (); Daofactory.save (); Console.WriteLine ("I'm the Factory method ."); Iocmethod (); //IOC Method "Console.ReadLine (); } /// <summary> ///this solves the problem of program-to-persondao coupling to a certain extent, but actually does not solve the coupling completely,///just put the coupling in the XML file, through a container when needed to form this dependency,///that is, inject the required interface implementation into the class that needs it. I personally think that the IOC model can be seen as a distillation of the factory model,///The IOC can be seen as a large factory, except that the objects to be generated in this large factory are defined in the XML file. /// </summary> Private Static voidIocmethod () {Iapplicationcontext CTX=Contextregistry.getcontext (); Ipersondao DAO= CTX. GetObject ("Persondao") asIpersondao; if(DAO! =NULL) {dao. Save (); Console.WriteLine ("I'm the IOC method ."); } } } Public InterfaceIpersondao {voidSave (); } Public classPersondao:ipersondao { Public voidSave () {Console.WriteLine ("Save Person"); } } Public Static classDataAccess { Public StaticIpersondao Createpersondao () {return NewPersondao (); } }}
<?XML version= "1.0" encoding= "Utf-8"?><Configuration> <configsections> <!--with the following spring.net node configuration is a one by one correspondence relationship - <sectiongroupname= "Spring"> < Sectionname= "Context"type= "Spring.Context.Support.ContextHandler, Spring.core"/> < Sectionname= "Objects"type= "Spring.Context.Support.DefaultSectionHandler, Spring.core" /> </sectiongroup> </configsections> <Startup> <supportedruntimeversion= "v4.0"SKU=". netframework,version=v4.0 "/> </Startup> <Spring> <Context> <ResourceURI= "Config://spring/objects"/> </Context> <Objectsxmlns= "Http://www.springframework.net"> <!--here, put all the nodes inside the container. - <Description>An example that demonstrates the simple IoC features.</Description> <!--name must be unique, the full name of the Type= class, where the assembly - <Objectname= "Persondao"type= "Test2.persondao, Test2"> </Object> </Objects> </Spring></Configuration>
Spring.net control Inversion (IoC) and environment configuration