您的位置:首页 > 编程语言 > Java开发

Spring.NET程序

2015-08-31 10:41 507 查看

第一个Spring.NET程序

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: