Spring.net main functions:
1.IoC: Control rollover (inversion of controls) understood as abstract factory
Rollover control: The right to create objects is controlled by the developer himself, and is transferred to the container.
2.DI: Dependency Injection (Dependency injection)
Dependency Injection: When an object is created through a container, the initialization of the object can inject default values (which can be complex types) to properties, parameters of the constructor method, and so on.
3.AOP: Aspect-oriented programming (similar: Pipelines, MVC filters, etc.)
First, the IOC sample demo:
1. New WinForm Project
2. Create a new folder in the solution Lib, used to store the DLLs and files used by spring.net
Spring.Core.dll, Common.Logging.dll (Spring.Core.dll internal use), Spring.Core.pdb, Spring.Core.xml
3. First add Spring.net core Dll:Spring.Core.dll and Common.Logging.dll references
4. Configure the app. Config file:
5. Code calls:
UsingSpring.context;UsingSpring.Context.Support;UsingSystem;UsingSystem.Windows.Forms;Namespace CZBK. heimaoa.springnetdemo{public partial class Form1:form {public Form1 () {InitializeComponent ();} private void button1_click ( object sender, EventArgs e) {Iapplicationcontext context = Contextregistry.getcontext (); // create container iuserinfoservice Userinfoservice = ( Iuserinfoservice) context. GetObject ( "userinfoservice "); MessageBox.Show (Userinfoservice.showmsg ()); } }}
Userinfoservice class:
UsingSystem;Using System.Collections.Generic; using System.linq; Using System.text; Using System.threading.tasks; Namespace CZBK. heimaoa.springnetdemo{public class Userinfoservice:iuserinfoservice {public string< Span style= "color: #000000;" > ShowMsg () {return " Hello world
Iuserinfoservice Interface:
Namespace czbk. heimaoa.springnetdemo{ Interface iuserinfoservice { string showmsg ();}}
6. Effect:
7.Demo Source Download: Click to download >>
Second, DI: Dependency Injection Sample Demo:
1. Add a complex Type:
Namespace czbk. heimaoa.springnetdemo{ class person { set; }}}
2. Modify the previous code:
UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;UsingSystem.Text;UsingSystem.Threading.Tasks;NamespaceCZBK. heimaoa.springnetdemo{PublicClass Userinfoservice:iuserinfoservice {public string Name {get; set public person person {setpublic string ShowMsg ( {return "hello World: "+ Name + age is: "+ person.age;}}
3. Modify the App. config configuration:
<?xml version="1.0"Encoding="Utf-8"?><configuration> <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> <context> <resource uri="Config://spring/objects"/> </context> <objects xmlns="Http://www.springframework.net"> <Object Name="Userinfoservice"Type="CZBK. HeiMaOA.SpringNetDemo.UserInfoService, CZBK. Heimaoa.springnetdemo"> <property name="Name"Value="Tom"/><property name="Person "ref=" person "/> </object> <object name= "person" Type= "CZBK. HeiMaOA.SpringNetDemo.Person, CZBK. Heimaoa.springnetdemo" age "Value=" 20 "/> </object> </objects> </spring></configuration>
4. Operating effect:
5. SOURCE download: Click to download >>
Third, Improve:
Place the Objects node configuration in app. Config into a separate XML file
1. Cut the objects node into the Objects.xml file:
<?xml version="1.0"Encoding="Utf-8"? ><objects xmlns="Http://www.springframework.net"> <Object Name="Userinfoservice"Type="CZBK. HeiMaOA.SpringNetDemo.UserInfoService, CZBK. Heimaoa.springnetdemo"> <property name="Name"Value="Tom"/> <property name="personref= "person"/></ Object><object name= " person" CZBK. HeiMaOA.SpringNetDemo.Person, CZBK. Heimaoa.springnetdemo" age "Value=" 20 "/></object> </objects>
2. Modify the App.confgi file:
<?xml version="1.0"Encoding="Utf-8"?><configuration> <configSections> <sectiongroup name="Spring"> <section name="Context"Type="Spring.Context.Support.ContextHandler, Spring.core"/> <section name="objects" spring.context.support.defaultsectionhandler, Spring.core "/> </sectiongroup > </configSections> <spring> <context> <resource uri= "config://spring/objects <resource uri= "file://objects.xml" /> <!--Specify the XML file location (here The program will find this file in the Bin\Debug or release directory, you need to modify the following XML file properties, to always copy to the output directory)--</context>
<objects xmlns= "Http://www.springframework.net" > <!--this node needs to be reserved-->
</objects>
</spring></configuration>
3. Modify the Objects.xml file properties:
4. Operating effect:
5. SOURCE download: Click to download >>
Iv. use of spring.net in MVC4
1.WEB Engineering Add DLL reference: Click to download the required DLL file >>
2. Create a new config folder and controller XML file under Web engineering:
3. Controller code:
Namespacewebapp.controllers{ Public class Userinfocontroller:controller {//// GET:/u serinfo/ //Iuserinfoservice userinfoservice = new Userinfoservice (); // modified to the following: CZBK. HeiMaOA.IBLL.IUserInfoService Userinfoservice { get; set;}//This completes the decoupling of the web layer and the BLL layer public ActionResult Index () { return View ();}}
4.controllers.xml File Configuration content:
<?xml version="1.0"Encoding="Utf-8"? ><objects xmlns="Http://www.springframework.net"> <Object type= "webapp.controllers.userinfocontroller,webapp "Singleton=" false " Userinfoservice "ref= "userinfoservice"/> <!--configuration userinfocontroller userinfoservice--> </ OBJECT>
<object type= "CZBK. HEIMAOA.BLL.USERINFOSERVICE,CZBK. Heimaoa.bll "singleton=" false "
</object> </objects>
5. Modify the Web. config file in the website to add the following nodes:
<configSections> <!--spring.net configuration- <sectiongroup name="Spring" > <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4 "/> <!--specified as mvc4--> </sectionGroup> </configSections> <!--spring.net configured-<spring > <context> <resource uri="file://~/Config/controllers.xml"/> <!--specify controller XML file-- > </context> </spring> <!--spring.net configuration ends -
6. SOURCE download: Click to download >>
V. Summary:
Spring.net essentially the bottom is to use reflection to read the configuration file, the need for changes in the future does not need to modify the code, directly modify the configuration file can be, convenient and flexible.
Vi. spring.net Chinese Help document (online version):
Click to download >>
Spring.net QuickStart: Controlling rollover, Dependency injection, tangent-oriented programming