http://www.cnblogs.com/GoodHelper/archive/2009/11/20/SpringNet_Index.html
1.配置
下载并引用spring.core,common.logging
<?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"> <description>一个简单的控制反转例子</description> <object id="PersonDao" type="SpringNetTest.PersonDao, SpringNetTest" /> </objects> </spring> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
using System; using Spring.Context; using Spring.Context.Support; namespace SpringNetTest { class Program { static void Main(string[] args) { IApplicationContext ctx = ContextRegistry.GetContext(); IPersonDao dao = ctx.GetObject("PersonDao") as IPersonDao; if (dao != null) { dao.Save(); Console.WriteLine("我是IoC方法"); Console.Read(); } } } public interface IPersonDao { void Save(); } public class PersonDao : IPersonDao { public void Save() { Console.WriteLine("保存 Person"); } } }
2.属性注入
<?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"> <description>一个简单的控制反转例子</description> <object id="computer" type="SpringTest1.Computer, SpringTest1" /> <object id="modernPerson" type="SpringTest1.ModernPerson, SpringTest1"> <property name="Tool" ref="computer"/> </object> </objects> </spring> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
using System; using Spring.Context; using Spring.Context.Support; namespace SpringTest1 { class Program { static void Main(string[] args) { IApplicationContext ctx = ContextRegistry.GetContext(); Person person = (Person)ctx.GetObject("modernPerson"); person.Work(); Console.ReadLine(); } } public abstract class Person { public abstract void Work(); } public interface ITool { void UseTool(); } public class Computer : ITool { public void UseTool() { Console.WriteLine("Computer"); } } public class ModernPerson : Person { public ITool Tool { get; set; } public override void Work() { Tool.UseTool(); Console.WriteLine("Use tool"); } } }
3.属性注入,值类型的注入,构造函数注入
<?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"> <description>一个简单的控制反转例子</description> <object id="person" type="SpringTest2.Person, SpringTest2"> <!--属性值类型注入--> <property name="Name" value="Liu Dong"/> <property name="Age" value="27"/> <!--内联对象注入--> <property name="Friend"> <object type="SpringTest2.Person, SpringTest2"> <property name="Name" value="Beggar"/> <property name="Age" value="23"/> <property name="Friend" ref="person"/> </object> </property> </object> <object id="personDao" type="SpringTest2.PersonDao, SpringTest2"> <!--构造器注入--> <constructor-arg name="argPerson" ref="person"/> <constructor-arg name="intProp" value="1"/> </object> </objects> </spring> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
using System; using Spring.Context; using Spring.Context.Support; namespace SpringTest2 { class Program { static void Main(string[] args) { IApplicationContext ctx = ContextRegistry.GetContext(); PersonDao dao = ctx.GetObject("personDao") as PersonDao; dao.Get(); Console.ReadLine(); } } public class Person { public string Name { get; set; } public int Age { get; set; } public Person Friend { get; set; } } public class PersonDao { private Person argPerson; private int intProp; public PersonDao(Person argPerson, int intProp) { this.argPerson = argPerson; this.intProp = intProp; } public void Get() { //构造函数注入的整型参数 Console.WriteLine(string.Format("intProp:{0}", intProp)); //构造函数注入的Person Console.WriteLine(string.Format("argPerson Name:{0}", argPerson.Name)); Console.WriteLine(string.Format("argPerson Age:{0}", argPerson.Age)); //内联对象Friend Console.WriteLine(string.Format("argPerson Friend Name:{0}", argPerson.Friend.Name)); Console.WriteLine(string.Format("argPerson Friend Age:{0}", argPerson.Friend.Age)); //内联对象的循环引用 Console.WriteLine(string.Format("argPerson Friend Friend Name:{0}", argPerson.Friend.Friend.Name)); Console.WriteLine(string.Format("argPerson Friend Friend Age:{0}", argPerson.Friend.Friend.Age)); } } }
4.集合类型注入——ILIst类型,IDictionary类型
<?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"> <description>一个简单的控制反转例子</description> <object id="person" type="SpringTest3.Person, SpringTest3"> <!--空集合属性--> <property name="BestFriends"> <list element-type="SpringTest3.Person, SpringTest3"> <object type="SpringTest3.Person, SpringTest3"> <property name="Name" value="张三"/> </object> <object type="SpringTest3.Person, SpringTest3"> <property name="Name" value="李四"/> </object> <object type="SpringTest3.Person, SpringTest3"> <property name="Name" value="王五"/> </object> </list> </property> <!--System.Collections.IList注入 --> <property name="HappyYears"> <list> <value>1992</value> <value>1998 年</value> <ref object="oneYear"/> </list> </property> <!--System.Collections.IList<int>注入 --> <property name="Years"> <list element-type="int"> <value>1992</value> <value>1998</value> <value>2000</value> </list> </property> <!--System.Collections.IDictionary注入--> <property name="HappyDic"> <dictionary key-type="string" value-type="object"> <entry key="第一开心" value="每天都能睡一个好觉"/> <entry key="第二开心" value-ref="happy"/> </dictionary> </property> <!--System.Collections.IDictionary<object,object>注入--> <property name="HappyTimes"> <dictionary key-type="string" value-type="object"> <entry key="第一开心" value="每天都能睡一个好觉"/> <entry key="第二开心" value-ref="happy"/> </dictionary> </property> </object> <object id="oneYear" type="SpringTest3.OneYear,SpringTest3"/> <object id="happy" type="SpringTest3.Happy,SpringTest3"/> </objects> </spring> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Spring.Context; using Spring.Context.Support; using System.Collections; namespace SpringTest3 { class Program { static void Main(string[] args) { IApplicationContext ctx = ContextRegistry.GetContext(); Person person = ctx.GetObject("person") as Person; Console.WriteLine("BestFriends"); foreach (Person friend in person.BestFriends) { Console.WriteLine(friend.Name); } Console.WriteLine(); Console.WriteLine("IList"); foreach (var item in person.HappyYears) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine("泛型Ilist<int>"); foreach (int item in person.Years) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine("IDictionary"); foreach (DictionaryEntry item in person.HappyDic) { Console.WriteLine(item.Key + " 是 " + item.Value); } Console.WriteLine(); Console.WriteLine("泛型IDictionary<string,object>"); foreach (KeyValuePair<string, object> item in person.HappyTimes) { Console.WriteLine(item.Key + " 是 " + item.Value); } Console.ReadLine(); } } public class Happy { public override string ToString() { return "每天都开心,每天都有好心情"; } } public class OneYear { public override string ToString() { return "快乐的一年"; } } public class Person { public string Name { get; set; } public IList<Person> BestFriends { get; set; } public IList HappyYears { get; set; } public IList<int> Years { get; set; } public IDictionary HappyDic { get; set; } public IDictionary<string, object> HappyTimes { get; set; } } }
x.待续