0

1.     准备工作

安装Spring.Net类库,下载地址:

http://dist.springframework.org/release/NET/Spring.NET-1.2.0.exe

安装完成后,找到安装目录下的doc\schema\目录,复制所有xsd格式文件到Visual Studioschema目录下,就可以在配置XML的时候使用VS的代码提示了。

VS2005----X:\Program Files\Microsoft Visual Studio 8\Xml\Schemas 

VS2008----X:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas

VS2010。。。。。

 

2.     测试一个。。。

解决方案资源管理器:

Spring.Net配置及示例 - 盡頭 - 深山憨娃

 添加Spring.Net库引用:

Spring.Core.dllSpring.Aop.dll

引用的dll文件在Spring.Net安装目录下bin\net\2.0\release\中。

Web.config配置文件中主要配置引用哪个配置文件(也可以直接在Web.config中配置,较为繁琐(主要是因为Web.config文件中原本就有很多配置……),我会用注释进行说明)

Web.config内容:

<configSections>

其他默认配置。。。。。。。。

<sectionGroup name="spring">

      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>

<!--配置在Web.config中需要这个节点

<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>-->

</sectionGroup>

</configSections>

 

  <spring>

<!--

配置在Web.config中需要指定type

<context type="Spring.Context.Support.XmlApplicationContext, Spring.Core">

-->

<!--配置在其他xml中不需要指定type-->

    <context>

      <!--<resource uri="config://spring/objects"/>(配置在Web.config中方法)-->

      <resource uri="~/spring.xml.config"/><!--指定其他配置文件-->

    </context>

 

<!--

Web.config中配置

<objects xmlns="http://www.springframework.net">

      <object name="child" type="WebApplication1.Child">

        <property name="name" value="超超"/>

      </object>

    </objects>-->

  </spring>

 

 

spring.xml.config文件内容:

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

<objects xmlns="http://www.springframework.net">

  <object name="child" type="WebApplication1.Child">

    <property name="name" value="超超 "/>

  </object>

</objects>

 

 

 

Child类:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace WebApplication1

{

    public class Child

    {

        public string Name { getset; }

 

        public string SayHello()

        {

            return this.Name + "say hello";

        }

    }

}

 

 

主程序:

protected void Page_Load(object sender, EventArgs e)

{

        IApplicationContext context = ContextRegistry.GetContext();

        Child child = context.GetObject("child"as Child;

        Response.Write(child.SayHello());

}

 

运行结果:

Spring.Net配置及示例 - 盡頭 - 深山憨娃

http://weilai.men.blog.163.com/blog/static/1424008462012924564021/
关闭 返回顶部
联系我们
Copyright © 2011. 聚财吧. All rights reserved.