旧地址:http://blog.canself.com/springnet_creatobject/
关于Spring.net及IoC、DI等的介绍,此处就不详述——偶不擅长啦。下面直入主题:首先说说基础配置。
首先是添加引用了,现在微软的nuget操作起来很方便了,直接在nuget找到Spring.Core的项,安装即可,至于其他的Spring.NET相关的dll可以需要时添加使用,如用AOP时,添加Spring.AOP。
下面就是创建对象了:
首先,可以使用最简单的例子,将所有配置都放在app.config中,类及配置文件见下方代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class AppConfigObject { public AppConfigObject () { } public void Run () { Console.WriteLine("AppConfigObject运行!" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?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 id ="AppConfigObject" type ="SpringNETCreateObject.AppConfigObject, SpringNETCreateObject" /> </objects > </spring > </configuration >
使用时如下即可:
1 2 3 IApplicationContext ctx = ContextRegistry.GetContext(); AppConfigObject dao = ctx.GetObject("AppConfigObject" ) as AppConfigObject; dao.Run();
但是,软件开发中,为了方便管理,往往添加单独的xml配置。
如:添加单独的Objects.xml
1 2 3 4 5 6 7 8 9 <?xml version="1.0" encoding="utf-8" ?> <objects xmlns ="http://www.springframework.net" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd" > <object id ="XMLObject" type ="SpringNETCreateObject.XMLObject, SpringNETCreateObject" > </object > </objects >
然后再app.config的spring/context下添加
1 2 <resource uri ="file://Objects.xml" />
或者直接在代码里使用xml
1 2 3 IApplicationContext ctx1 = new XmlApplicationContext( "file://Objects.xml" );
对象的创建则使用ApplicationContext对象。
最后,讲一些特殊类型的对象创建配置。其中包括嵌入类型、泛型类型。
其中需要在app.config的spring/context下添加
1 2 3 <resource uri ="assembly://DAO/DAO/DAOObjects.xml" />
objects配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 <?xml version="1.0" encoding="utf-8" ?> <objects xmlns ="http://www.springframework.net" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd" > <object id ="normaldao" type ="DAO.NormalDAO,DAO" /> <object id ="nesteddao" type ="DAO.NestedDAO+InnerDAO,DAO" /> <object id ="genericdao" type ="DAO.GenericDAO< int>,DAO" /> </objects >
相关类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 public interface BaseDAO { void Run () ; } public class NormalDAO : BaseDAO { public void Run () { Console.WriteLine("NormalDAO运行" ); } } public class NestedDAO { public class InnerDAO : BaseDAO { public void Run () { Console.WriteLine("NestedDAO运行" ); } } } public class GenericDAO <T > : BaseDAO { public void Run () { Console.WriteLine("泛型({0})运行" , this .GetType()); } }
使用:
1 2 3 4 5 6 7 8 BaseDAO normaldao = ctx.GetObject("normaldao" ) as BaseDAO; normaldao.Run(); BaseDAO nesteddao = ctx.GetObject("nesteddao" ) as BaseDAO; nesteddao.Run(); BaseDAO genericdao = ctx.GetObject("genericdao" ) as BaseDAO; genericdao.Run();
项目地址:https://springnetstudy.codeplex.com