LYWILL设计运营 - 网站运营与推广、开发技术、成功项目展示。

Spring.NET环境搭建详解

  一、环境下载及安装  
到Spring的官方网站下载Spring.NET框架的安装文件(Spring.NET-1.3.2.exe),下载并解压后就可以了。

我们使用Spring.NET框架经常用到的一下几个文件:
Common.Logging.dll(必要)
Spring.Core.dll(必要)
Spring.Data.dll
Spring.Aop.dll(可选)
Spring.Data.NHibernate21.dll
Spring.Web.dll
 

 
在基于XML的工厂中,这些对象定义表现为一个或多个<object>子节点,它们的父节点必须是<objects>(objects节点的xmlns元素是必需的,必须根据不同的应用添加不同的命名空间,以便有IDE的智能提示(见Spring.NET中文手册)。
XML/HTML代码
  1. <objects xmlns="http://www.springframework.net"    
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  3.     xsi:schemaLocation="http://www.springframework.net   
  4.         http://www.springframework.net/xsd/spring-objects.xsd">  
  5.   <object id="" type="">  
  6.      
  7.   </object>  
  8.   <object id="." type="">  
  9.      
  10.   </object>  
  11.   
  12.      
  13. </objects>  
 

同样也可以找到Spring.NET解压目录下的Spring.NET-1.3.2\doc\schema,把里面的几个.xsd复制到VS2008安装目录下的Microsoft Visual Studio 9.0\Xml\Schemas文件夹。

必要的时候可以安装建立Spring.NET程序的模板Spring.NET-1.3.2\dev-support\vs.net-2008\install-templates.bat。
 

  二、建立一个Spring.NET应用程序
我们新建一个Objects.xml的文件,然后从Spring.NET手册中复制来一段配置模板
 

XML/HTML代码
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2.   
  3. <objects xmlns="http://www.springframework.net"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.net   
  6.         http://www.springframework.net/xsd/spring-objects.xsd">  
  7.      
  8.   <object id="PersonDao" type="FirstSpringNetApp.PersonDao, FirstSpringNetApp">  
  9.   
  10.   </object>  
  11.      
  12. </objects>  

目前我找到了实例化Spring.NET容量的两种方式:
1.实际物理路径
   IResource input = new FileSystemResource(@"D:\Objects.xml"); //实际物理路径
          IObjectFactory factory = new XmlObjectFactory(input);

2.程序集下寻找配置文件

C#代码
  1. string[] xmlFiles = new string[]    
  2. {   
  3.     "file://Objects.xml"  
  4. };   
  5. IApplicationContext context = new XmlApplicationContext(xmlFiles);   
  6.   
  7. IObjectFactory factory = (IObjectFactory)context;   
  8. Console.ReadLine();  
 

  目前我一般采用后者(程序集下寻找配置文件),这样维护起来比较方便。
  这种方式需满足URI语法。
  file://文件名
  assembly://程序集名/命名空名/文件名


然而更好的方式是在配置文件App.config或Web.config添加自定义配置节点
在配置文件中要引入<objects xmlns="
http://www.springframework.net"/>命名空间,否则程序将会无法实例化Spring.NET容器。
 

XML/HTML代码
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   
  4.   <configSections>  
  5.     <sectionGroup name="spring">  
  6.       <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />  
  7.       <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />  
  8.     </sectionGroup>  
  9.   </configSections>  
  10.   
  11.   <spring>  
  12.   
  13.     <context>  
  14.       <resource uri="assembly://FirstSpringNetApp/FirstSpringNetApp/Objects.xml"/>  
  15.       <resource uri="config://spring/objects" />  
  16.     </context>  
  17.     <objects xmlns="http://www.springframework.net"/> <!--必要-->  
  18.   </spring>  
  19.   
  20. </configuration>  
 
 
调用实例:
C#代码
  1. namespace FirstSpringNetApp   
  2. {   
  3.     class Program   
  4.     {   
  5.         static void Main(string[] args)   
  6.         {   
  7.             AppRegistry();   
  8.             Console.ReadLine();   
  9.         }   
  10.         static void AppRegistry()   
  11.         {   
  12.             IApplicationContext ctx = ContextRegistry.GetContext();   
  13.             Console.WriteLine(ctx.GetObject("PersonDao").ToString());   
  14.         }   
  15.         static void XmlSystem()   
  16.         {   
  17.             string[] xmlFiles = new string[]    
  18.             {   
  19.                 //"file://Objects.xml"  //, 文件名   
  20.                 "assembly://FirstSpringNetApp/FirstSpringNetApp/Objects.xml"  //程序集   
  21.             };   
  22.             IApplicationContext context = new XmlApplicationContext(xmlFiles);   
  23.             IObjectFactory factory = (IObjectFactory)context;   
  24.             Console.WriteLine(factory.GetObject("PersonDao").ToString());   
  25.         }   
  26.         static void FileSystem()   
  27.         {   
  28.             IResource input = new FileSystemResource(@"D:\Objects.xml");  //实际物理路径   
  29.             IObjectFactory factory = new XmlObjectFactory(input);   
  30.             Console.WriteLine(factory.GetObject("PersonDao").ToString());   
  31.         }   
  32.     }   
  33. }   
  34.    

 

标签: Spring.NET

作者:lywill 分类:开发技术 浏览:4199 评论:0