标签:
namespace Spring.Net { class Program { //构造器注入 static void Main(string[] args) { IApplicationContext ctx = ContextRegistry.GetContext(); //通过容器创建对象 IUser _user = ctx.GetObject("User") as IUser; _user.Show(); Console.ReadKey(); } } public interface IUser { string Name { get; set; } void Show(); } public class User : IUser { public string Name { get; set; } public void Show() { Console.WriteLine("我是User的Show方法"); } } }
<?xml version="1.0" encoding="utf-8" ?> <configuration> <!--一定要在紧跟着configuration下面添加--> <configSections> <!--跟下面Spring.Net节点配置是一一对应关系--> <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> <!--name 必须要唯一的,type=类的全名称,所在的程序集--> <object name="User" type="Spring.Net.User, Spring.Net"> </object> </objects> </spring> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
namespace Spring.Net { class Program { static void Main(string[] args) { IApplicationContext ctx = ContextRegistry.GetContext(); //通过容器创建对象 IUser _user = ctx.GetObject("User") as IUser; IPeople _people = ctx.GetObject("People") as IPeople; Test _test = ctx.GetObject("Test") as Test; Console.WriteLine(_user.Name); Console.WriteLine(_user.Age); Console.WriteLine("--------------------------------------------"); Console.WriteLine(_people.Man.Name); Console.WriteLine(_people.Man.Age); Console.WriteLine("--------------------------------------------"); Console.WriteLine(_test.Name); Console.WriteLine(_test.Age); Console.ReadKey(); } } public interface IUser { string Name { get; set; } int Age { get; set; } void Show(); } public class User : IUser { public string Name { get; set; } public int Age { get; set; } public void Show() { Console.WriteLine("我是User的Show方法"); } } public interface IPeople { IUser Man { get; set; } } public class People : IPeople { public IUser Man { get; set; } } public class Test { public string Name{get;set;} public int Age{get;set;} public Test(string name, int age) { Name = name; Age = age; } } }
<?xml version="1.0" encoding="utf-8" ?> <configuration> <!--一定要在紧跟着configuration下面添加--> <configSections> <!--跟下面Spring.Net节点配置是一一对应关系--> <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> <object name="User" type="Spring.Net.User, Spring.Net"> <!--01属性注入-值类型--> <property name="Name" value="Linq"></property> <property name="Age" value="25"></property> </object> <object name="People" type="Spring.Net.People, Spring.Net"> <!--02属性注入-引用类型--> <property name="Man" ref="User"></property> </object> <object name="Test" type="Spring.Net.Test, Spring.Net"> <!--03构造函数注入--> <constructor-arg name="name" value="config配置"></constructor-arg> <constructor-arg name="age" value="25"></constructor-arg> </object> </objects> </spring> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
namespace Spring.Net { class Program { static void Main(string[] args) { IApplicationContext ctx = ContextRegistry.GetContext(); ObjectFactory dao = (ObjectFactory)ctx.GetObject("objectFactory"); //查询方法注入 //查询方法注入就利用了这些功能。个人感觉查询方法注入类似抽象工厂, //为之不同的是,可以不用写抽象的实现代码,通过配置文件动态的切换组件。 dao.CreatePersonDao().Save(); //事件注入 Door door = (Door)ctx.GetObject("door"); door.OnOpen("Opening!"); Console.WriteLine(); Console.Read(); } } public abstract class ObjectFactory { //或者可以是一个虚方法 public abstract PersonDao CreatePersonDao(); } public class PersonDao { public void Save() { Console.WriteLine("保存数据"); } } //先定义一个委托 public delegate string OpenHandler(string arg); public class Door { public event OpenHandler OpenTheDoor; public void OnOpen(string arg) { //调用事件 if (OpenTheDoor != null) { Console.WriteLine(OpenTheDoor(arg)); } } } public class Men { public string OpenThisDoor(string arg) { return "参数是:" + arg; } } }
<?xml version="1.0" encoding="utf-8" ?> <configuration> <!--一定要在紧跟着configuration下面添加--> <configSections> <!--跟下面Spring.Net节点配置是一一对应关系--> <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> <!--查询方法--> <object id="personDao" type="Spring.Net.PersonDao, Spring.Net" singleton="false"/> <object id="objectFactory" type="Spring.Net.ObjectFactory, Spring.Net"> <lookup-method name="CreatePersonDao" object="personDao"/> </object> <!--事件注入--> <object id="men" type="Spring.Net.Men, Spring.Net"> <listener event="OpenTheDoor" method="OpenThisDoor"> <ref object="door"/> </listener> </object> <object id="door" type="Spring.Net.Door, Spring.Net" /> </objects> </spring> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
标签:
原文地址:http://www.cnblogs.com/lgxlsm/p/5410944.html