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>

五. 整个项目的下载

狠狠地点击这里下载

原文地址:https://www.cnblogs.com/haogj/p/2614091.html