Spring.NET 1.3.2 集成 NHibernate 3.2 - 2 - 配置使用 Spring.NET 的網站


我們現在 Visual Studio 中創建一個網站應用程序,為了方便檢查,在網站中增加一個名為 index.aspx 的頁面,並設為起始頁。

首先解決 Sping.NET 在網站中的配置問題。

Spring.NET 大量使用配置文件,如果你願意的話,也可以使用代碼進行配置,不過,我們還是使用傳統的配置文件方式。

一. 添加程序集引用

首先,我們需要在網站中添加對於 Spring.NET 程序集的引用,最基本的是兩個程序集 Spring.Core.dll 和 Spring.Web.dll。

Spring.Core.dll 是整個 Spring.NET 的核心程序集,而 Spring.Web.dll 則對於網站開發提供支持。

二. 配置 Spring.NET

為了在啟動網站的時候,能夠自動創建 Spring.NET 的應用程序環境,需要在網站的配置文件 web.config 中進行設置。

1. 配置 Spring.NET 的 WebSupportModule,這個 Module 可以重建 Spring.NET 的應用程序環境,配置方式與普通的 Module 相同。

在網站的 web.config 配置文件中,增加 <httpModules> 配置節。

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpModules>
      <!-- Spring 提供的 Module  -->
      <add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/>
    </httpModules>
  </system.web>

如果這個時候,運行網站,應該會看到如下的頁面。

再刷新一下,又成為下面的樣子。

錯誤的原因是沒有在 web.config 中找到需要的 Spring.NET 配置信息。

解決這個問題,需要在 web.config 增加一個新的配置節,這個配置節由 Spring.NET 定義,需要添加在 web.config 的 configuration 的頭部。

<configuration>
  
  <configSections>
    <!-- Spring 的配置 -->
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/>
    </sectionGroup>
  </configSections>
  <spring>
    <context>
    </context>
  </spring>

現在運行程序,應該可以正常看到頁面內容了。

為了能夠為頁面對象進行注入,需要將創建頁面對象的工作從默認的 ASP.NET 網站中替換到 Spring.NET 中,這需要配置一系列處理程序。

    <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>

現在,頁面應該可以正常瀏覽了。從此以后的頁面將通過 Spring.NET 創建與管理。

三. 定義對象

首先,我們定義一個類,表示框架的信息。

    public class Framework
    {
        public string Name { set; get; }
    }

在頁面上定義一個 Label ,顯示框架的名稱。

    <div>
        <h1><asp:Label runat="server" ID="lblFramework"></asp:Label></h1>
    </div>

然后,在 index.aspx 中定義一個注入點,准備使用這個對象。

    public partial class index : System.Web.UI.Page
    {
        // 定義一個注入點
        public Framework Framework { set; get; }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblFramework.Text = this.Framework.Name;
        }
    }

下面在 Spring.NET 的配置使用。

定義對象主要有兩種方式,直接定義在 web.config 中,或者定義在外部的配置文件中。

1. 直接定義在 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="Spring_Web_1.Framework">
        <property name="Name" value="Spring.NET"/>
      </object>
      
      <!-- 頁面對象 -->
      <object type="~/index.aspx">
        <!-- ref 表示引用的對象 -->
        <property name="Framework" ref="framework"/>
      </object>
    </objects>
  </spring>

注意上面的 resource 配置元素,說明 Spring.NET 的應用程序上下文中需要使用定義在配置文件的 spring 配置節中的 objects 配置元素來定義對象。

在 objects 配置節中,我們定義了兩個對象 corporation 和 一個頁面。

對於 corporation 我們對 Name 屬性直接使用值進行注入,使用的屬性為 value 方式。

而對於頁面,我們使用引用方式,使用的配置屬性為 ref,值設置為另一個對象的 id。

現在,運行程序,我們應該可以看到這樣的頁面。

2. 在單獨的配置文件中配置對象。

在網站中創建一個名為 Config 的文件夾,以保存獨立的配置文件。

在 Config 文件夾中,創建一個名為 objects.xml 的 Xml 配置文件。添加名為 objects 的根元素,添加默認命名空間 xmlns="http://www.springframework.net",還記得在上一篇文件中,將架構文件添加到 Visual Studio 中嗎?現在有用了。

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  
</objects>

添加原來對象定義到這里。

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  
  <object id="framework" type="Spring_Web_1.Framework">
    <property name="Name" value="Spring.NET"/>
  </object>

  <!-- 頁面對象 -->
  <object type="~/index.aspx">
    <!-- ref 表示引用的對象 -->
    <property name="Framework" ref="framework"/>
  </object>

</objects>

 

將原來在 Web.config 中配置的 objects 配置節刪除,將原來 context 配置節中的配置替換為如下的內容。

    <context>
      <resource uri="~/Config/objects.xml"/>
      <!--<resource uri="config://spring/objects"/>-->
    </context>

現在的 uri 中是外部配置文件的路徑了。

重新運行網站,你會看到網站依然在正常運行了。

四. 完整的配置文件

web.config 文件內容

<?xml version="1.0" encoding="utf-8"?>

<!--
  有關如何配置 ASP.NET 應用程序的詳細消息,請訪問
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  
  <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/objects.xml"/>
      <!--<resource uri="config://spring/objects"/>-->
    </context>
    <!-- 直接定義在 web.config 中的對象 -->
    <!--<objects>
      <object id="framework" type="Spring_Web_1.Framework">
        <property name="Name" value="Spring.NET"/>
      </object>
      
      --><!-- 頁面對象 --><!--
      <object type="~/index.aspx">
        --><!-- ref 表示引用的對象 --><!--
        <property name="Framework" ref="framework"/>
      </object>
    </objects>-->
  </spring>

  
  <system.web>
    <compilation debug="true" 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>

objects.xml 文件的內容

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  
  <object id="framework" type="Spring_Web_1.Framework">
    <property name="Name" value="Spring.NET"/>
  </object>

  <!-- 頁面對象 -->
  <object type="~/index.aspx">
    <!-- ref 表示引用的對象 -->
    <property name="Framework" ref="framework"/>
  </object>

</objects>

五. 整個項目的下載

狠狠地點擊這里下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM