一、開發環境 系統:Win10 編譯器:VS2013 .net版本:.net framework4.5 二、涉及程式集 Spring.Core.dll 1.3.1 Common.Loggin.dll 三、開發過程 1.項目結構 2.編寫Person.cs namespace SpringNetSet... ...
一、開發環境
系統:Win10
編譯器:VS2013
.net版本:.net framework4.5
二、涉及程式集
Spring.Core.dll 1.3.1
Common.Loggin.dll
三、開發過程
1.項目結構
2.編寫Person.cs
namespace SpringNetSetDi { public class Person { public string RealName { get; set; } public string NickName { get; set; } public string LoginName { get; set; } public string ShowName { get; set; } } }
3.編寫Animal.cs
namespace SpringNetSetDi { public class Animal { public string Name { get; set; } public IList TypeList { get; set; } } }
4.編寫Zoo.cs
namespace SpringNetSetDi { public class Zoo { public List<Animal> AnimalList { get; set; } } }
6.配置文件App.config
<?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"></resource> </context> <objects xmlns="http://www.springframework.net"> <!--01設置空值--> <object name="person" type="SpringNetSetDi.Person,SpringNetSetDi"> <property name="ShowName" value="Kimisme"></property> <property name="RealName" value=""></property> <property name="NickName"> <value></value> </property> <property name="LoginName"> <null/> </property> </object> <!--02IList類型註入--> <object name="animal" type="SpringNetSetDi.Animal,SpringNetSetDi"> <property name="TypeList"> <list element-type="string"> <value>哺乳類</value> <value>鳥類</value> <value>爬行類</value> <value>昆蟲類</value> <value>兩棲類</value> </list> </property> </object> <!--03泛型List註入--> <object name="zoo" type="SpringNetSetDi.Zoo,SpringNetSetDi"> <property name="AnimalList"> <list element-type="SpringNetSetDI.Animal,SpringNetSetDi"> <object type="SpringNetSetDi.Animal,SpringNetSetDi"> <property name="Name" value="Eagle"></property> <property name="TypeList"> <list element-type="string"> <value>鳥類</value> </list> </property> </object> </list> </property> </object> </objects> </spring> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
7.控制台代碼
namespace SpringNetSetDi { class Program { static void Main(string[] args) { IApplicationContext context = ContextRegistry.GetContext(); Person person = context.GetObject("person") as Person; Animal animal = context.GetObject("animal") as Animal; Zoo zoo = context.GetObject("zoo") as Zoo; Console.ReadKey(); } } }
四、說明
這邊有個坑,花了我好長時間,具體解決看 問題彙總