Spring.NET環境准備
pring.NET 1.3.2下載地址:http://down.51cto.com/data/861700
下載后解壓
Spring.NET-1.3.2.7z:這個里面有我們需要用到的所有東西。
Spring.NET-1.3.2.exe:安裝文件
Spring.NET-1.3.2-API.chm:幫助文檔
NHibernate 3.2 的下載地址:
http://sourceforge.net/projects/nhibernate/files/NHibernate/3.2.0GA/
點擊上面紅框就可以進行下載了。
1、我們先點擊安裝文件Spring.NET-1.3.2.exe進行安裝,安裝過程點擊下一步就可以了。
完成后,你會看到如下界面:
2、打開Spring.NET的安裝目錄,可以看到如下界面:
3、展開Spring.Net安裝目錄,
主要文件夾說明:
lib:常用的程序集,其中包含了 NHibernate 3.2 的程序集
schema:Xml 的架構文件 ,提供XML的智能感知功能。操作說明:將 schema 中的 .xsd文件 復制到 Visual Studio 的安裝目錄下的 Xml\Schemas 文件夾中就可以實現xml智能提示了
配置Spring.Net網站
1、新建一個web網站,添加一個Index.aspx頁面。
2、添加程序集 Spring.Core.dll 和 Spring.Web.dl,Common.Logging.dll的引用
為了方便管理,新建一個BLL文件夾,然后從Spring.Net的安裝目錄拷貝這三個dll程序集過來,然后添加對這三個程序集的引用。
我用的是VS2010,所以拷貝4.0目錄下面的dll文件。
Spring.NET\Spring.NET\Spring.NET-1.3.2\Spring.NET\bin\net\4.0\release目錄下面
3、配置web.config
在網站的 web.config 配置文件中,進行如下配置
- <configuration>
- <configSections>
- <!-- Spring 的配置 -->
- <sectionGroup name="spring">
- <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
- </sectionGroup>
- </configSections>
- <spring>
- <context>
- </context>
- </spring>
- <system.web>
- <compilation debug="false" targetFramework="4.0" />
- <httpModules>
- <!--Spring 提供的 Module-->
- <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
- </httpModules>
- <httpHandlers>
- <!--Spring 提供的處理程序-->
- <add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>
- <!--取消 Spring.NET 對於 Web 服務的處理-->
- <add verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web"/>
- <add verb="*" path="ContextMonitor.ashx" type="Spring.Web.Support.ContextMonitor, Spring.Web"/>
- <add verb="*" path="*.ashx" type="Spring.Web.Support.DefaultHandlerFactory, Spring.Web"/>
- </httpHandlers>
- </system.web>
- </configuration>
現在,頁面應該可以正常瀏覽了。從此以后的頁面將通過 Spring.NET 創建與管理。
第一個程序“Hello,World”
1、新建一個類Framework.cs
- public class Framework
- {
- public Framework()
- {
- //
- //TODO: 在此處添加構造函數邏輯
- //
- }
- public string Name { set; get; }
- }
2、在Index.aspx頁面添加一個label
- <div>
- <h1><asp:Label runat="server" ID="lblFramework"></asp:Label></h1>
- </div>
3、Index.aspx.cs
- public partial class Index : System.Web.UI.Page
- {
- // 定義一個注入點
- public Framework FrameworkName { set; get; }
- protected void Page_Load(object sender, EventArgs e)
- {
- this.lblFramework.Text = this.FrameworkName.Name;
- }
- }
定義對象主要有兩種方式,直接定義在 web.config 中,或者定義在外部的配置文件中。
4、直接定義在 web.config 中,使用 Spring.Context.Support.DefaultSectionHandler。這樣可以在配置文件中直接定義。
- <configSections>
- <!-- Spring 的配置 -->
- <sectionGroup name="spring">
- <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
- <!-- 支持在 web.config 中定義對象 -->
- <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
- </sectionGroup>
- </configSections>
- <spring>
- <context>
- <resource uri="config://spring/objects"/>
- </context>
- <!-- 直接定義在 web.config 中的對象 -->
- <objects>
- <object id="framework" type="Framework"><!--類名-->
- <property name="Name" value="Hello,world"/><!--屬性名稱,值-->
- </object>
- <!-- 頁面對象 -->
- <object type="~/Index.aspx">
- <!-- ref 表示引用的對象 -->
- <property name="FrameworkName" ref="framework"/><!--Index.aspx頁面的屬性名稱-->
- </object>
- </objects>
- </spring>
5、瀏覽Index.aspx
6、在單獨的配置文件中配置對象。
在網站中創建一個名為 Config 的文件夾,以保存獨立的配置文件。
在 Config 文件夾中,創建一個名為 objects.xml 的 Xml 配置文件。添加名為 objects 的根元素,添加默認命名空間 xmlns="http://www.springframework.net"
找到如下架構文件,復制到vs安裝目錄:C:\Program Files (x86)\Microsoft Visual Studio 10.0\Xml\Schemas
這樣,我們在xml文件中就具備智能感知功能了。
添加原來對象定義到這里。
- <?xml version="1.0" encoding="utf-8" ?>
- <objects xmlns="http://www.springframework.net"><!--默認命名空間-->
- <object id="framework" type="Framework">
- <!--類名-->
- <property name="Name" value="Hello,China"/>
- <!--屬性名稱,值-->
- </object>
- <!-- 頁面對象 -->
- <object type="~/Index.aspx">
- <!-- ref 表示引用的對象 -->
- <property name="FrameworkName" ref="framework"/>
- <!--Index.aspx頁面的屬性名稱-->
- </object>
- </objects>
將原來在 Web.config 中配置的 objects 配置節刪除,將原來 context 配置節中的配置替換為如下的內容。
- <context>
- <resource uri="~/Config/objects.xml"/>
- <!--<resource uri="config://spring/objects"/>-->
- </context>
6、重新瀏覽Index.aspx