This seems, perhaps, perhaps, probably, should, maybe really can be regarded as the legendary spring.net.previously wrote an article called "This seems, maybe, probably, should, maybe really is the legend of the Interface programming bar", it seems to be in the comments promised later there will be an article called "article this seems, perhaps, probably, should, Maybe really is the legendary spring.net bar ". Then, now in our holiday time, probably can be considered to accept the promise of it. let's introduce the IOC. What about it, the Chinese name is control reversal. English is inversion of Control. Spring.net is a framework of the IOC. The original object is managed by a class, such as to create an instance of this class, and we will new this class. With the IOC we do not need to use this class to go to new, or to manage its corresponding object without this class. It was given to our IOC framework to manage these. This allows for loose coupling without the use of classes to manage. Mr. Liu Dong has written a series of spring.net that interested friends can look at. I'm going to write a small case here. First, create a form application, create a button on the form, and change the output type of the form application to a console application (if you do not change to a console application, you will not be able to output the results we want). Create a class and an interface. Write classes and interfaces to prepare for the subsequent invocation of the instance.
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceSpringNetStudy10248 {9 classMutudu:imutuduTen { One Public voidShow () A { -Console.WriteLine ("Xiao du succeeded in writing a spring.net case ." ); - } the } -}Mutudu
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceSpringNetStudy10248 {9 InterfaceImutuduTen { One voidShow (); A } -}Imutudu
because we need to use third-party class libraries. Then we have to first add the spring.net third-party class library to our project first. We'll create a new folder in our project to copy the third-party libraries we need. The project then adds references to these third-party class libraries. There are two DLLs that we have to refer to, and the other is Common.Logging.dll. The other one is Spring.Core.dll. These two are essential. if it's less, it will (I used to quote one less): third-party class libraries we are quoting in. We have said that the class is to be managed by our IOC container, what about our IOC container? We need to get the IOC container and then the IOC container to manage it. For this, we are initializing the IOC container by configuration. That is, initializing the iapplicationcontext. Personally, the status of this class is somewhat similar to the context of EF, a bit of a connecting point. So modify App. Config.
1<?xml version="1.0"encoding ="Utf-8"?>2<configuration>3< configsections>4< sectiongroup name ="Spring">5< section name ="Context"Type="Spring.Context.Support.ContextHandler, Spring.core"/>6< section name ="Objects"Type="Spring.Context.Support.DefaultSectionHandler, Spring.core"/>7</sectiongroup>8</configsections>9< spring>Ten< context> One< Resource URI ="config://spring/objects"/> A</context> -< objects xmlns ="http://www.springframework.net"> -< Description>an example that demonstrates simple IoC features.</description> the</objects> -</spring> -</configuration>
App.
Another thing to say is that in the configuration file, we configure the objects used in the program through the child nodes of the objects node, of course, the object node is located under the objects (with s) node. For example, we now need to configure the class that we will write, then configure the object node under the Objects node:
1<? XML version ="1.0"encoding ="Utf-8"?>2< configuration>3< configsections >4< sectiongroup name ="Spring">5< section name ="Context"Type ="Spring.Context.Support.ContextHandler, Spring.core"/>6< section name ="Objects"Type ="Spring.Context.Support.DefaultSectionHandler, Spring.core"/>7</sectiongroup >8</configsections >9< spring >Ten< context > One< Resource URI ="config://spring/objects"/> A</Context > -< objects xmlns ="http://www.springframework.net"> -< Description >an example that demonstrates simple IoC features.</description> the<!--The following is the configuration code that gives the class the window management, only this line. - -<ObjectName ="Mutudu"Type ="Springnetstudy1024.mutudu, SpringNetStudy1024"> </Object> -</Objects > -</Spring > +</configuration> -
class let IOC manage it.
through "iapplicationcontext ctx = contextregistry ." GetContext (); " This line of code gets the configured object below the node we configured. This line of code is pretty critical. So, let's look at the code in the button click event:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;Ten usingSpring.context; One usingSpring.Context.Support; A - namespaceSpringNetStudy1024 - { the Public Partial classForm1:form - { - PublicForm1 () - { + InitializeComponent (); - } + A Private voidPowerofioc_click (Objectsender, EventArgs e) at { - //What is returned here or given is the object of the container that is configured according to our content on the object node. -Iapplicationcontext CTX =Contextregistry. GetContext (); - //you can then get an instance of the object by getting it. -Imutudu Mutudu = (Imutudu) ctx. GetObject ("Mutudu"); - //call the method. in Mutudu. Show (); - } to } +}button click on the code in the event
Okay, good. The code is written, execute it, click the button, look at the power of spring.net (really no new OH):In that case, Xiao Du was successful in writing a little demo about spring.net. Reference configuration, it's almost there. There are also some attributes injected into the constructor. I can only say that maybe I'll write a continuation, yes, maybe. If there is any mistake, please treatise. Another, gentlemen, happy holidays.
This seems, perhaps, perhaps, probably, should, maybe really can be regarded as the legendary spring.net.