天天看點

Spring.NET教程(二)——環境搭建(基礎篇) (轉)

一、環境下載下傳及安裝  

  到Spring的官方網站下載下傳Spring.NET架構的安裝檔案(Spring.NET-1.3.0-RC1.exe)。目前Spring.NET最新的版本是1.3。下載下傳并解壓後就可以了。

  我們使用Spring.NET架構經常用到的一下幾個檔案:

  Common.Logging.dll(必要)

  Spring.Core.dll(必要)

  Spring.Data.dll

  Spring.Aop.dll(可選)

  Spring.Data.NHibernate21.dll

  Spring.Web.dll

  在以後的部落格裡我們會學習一些與NHibernate和Asp.NET MVC結合的例子,可以到Hibernate的官方網站和Asp.NET的官方網站下載下傳各自的架構安裝檔案。

  在基于XML的工廠中,這些對象定義表現為一個或多個<object>子節點,它們的父節點必須是<objects>(按:objects節點的xmlns元素是必需的,必須根據不同的應用添加不同的命名空間,以便有IDE的智能提示(見Spring.NET中文手冊)。

  Object.XML

<code>&lt;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"&gt;   &lt;object id="" type=""&gt;      &lt;/object&gt;   &lt;object id="." type=""&gt;      &lt;/object&gt;    &lt;/objects&gt;</code>

  同樣也可以找到Spring.NET解壓目錄下的Spring.NET-1.3.0-RC1\doc\schema,把裡面的幾個.xsd複制到VS2008安裝目錄下的Microsoft Visual Studio 9.0\Xml\Schemas檔案夾。

必要的時候可以安裝建立Spring.NET程式的模闆Spring.NET-1.3.0-RC1\dev-support\vs.net-2008\install-templates.bat

  二、建立一個Spring.NET應用程式

  我們建立一個Objects.xml的檔案,然後從Spring.NET手冊中複制來一段配置模闆

  Objects.xml

<code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;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"&gt;      &lt;object id="PersonDao" type="FirstSpringNetApp.PersonDao, FirstSpringNetApp"&gt;   &lt;/object&gt;    &lt;/objects&gt;</code>

  目前我找到了執行個體化Spring.NET容量的兩種方式:

  1.實際實體路徑

<code>         IResource input = new FileSystemResource(@"D:\Objects.xml");  //實際實體路徑             IObjectFactory factory = new XmlObjectFactory(input);</code>

  2.程式集下尋找配置檔案

<code>            string[] xmlFiles = new string[]              {                 "file://Objects.xml"             };             IApplicationContext context = new XmlApplicationContext(xmlFiles);             IObjectFactory factory = (IObjectFactory)context;             Console.ReadLine();</code>

目前我一般采用後者(程式集下尋找配置檔案),這樣維護起來比較友善。

  這種方式需滿足URI文法。

  file://檔案名

  assembly://程式集名/命名空名/檔案名

  然而更好的方式是在配置檔案App.config或Web.config添加自定義配置節點

  在配置檔案中要引入&lt;objects xmlns="http://www.springframework.net"/&gt;命名空間,否則程式将會無法執行個體化Spring.NET容器。

  App.config

<code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;configuration&gt;   &lt;configSections&gt;     &lt;sectionGroup name="spring"&gt;       &lt;section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" /&gt;       &lt;section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /&gt;     &lt;/sectionGroup&gt;   &lt;/configSections&gt;   &lt;spring&gt;     &lt;context&gt;       &lt;resource uri="assembly://FirstSpringNetApp/FirstSpringNetApp/Objects.xml"/&gt;       &lt;resource uri="config://spring/objects" /&gt;     &lt;/context&gt;     &lt;objects xmlns="http://www.springframework.net"/&gt; &lt;!--必要--&gt;   &lt;/spring&gt; &lt;/configuration&gt;               IApplicationContext ctx = ContextRegistry.GetContext();             Console.WriteLine(ctx.GetObject("PersonDao").ToString());</code>

  好了,Spring.NET的環境配置就先講到這裡。

javascript:void(0)