Spring.NET在非Web中的应用,以及封装Spring容器。

另外一篇随笔中已经有在Web中使用Spring.NET的文章了。这篇是在非Web中的应用。相对简单点。

我们引入Spring.Core,使用Spring.NET来实现工厂模式。过程如下:

 

a)        添加app.config应用程序配置文件。

b)        app.config中注册spring节点,加入一下配置代码(注意,是在<configuration>与</configuration>之间插入的。):

  <!-- 
  SP1:必须在.NET配置文件的<configSections>节点中注册这个类,
  注册了这个节点处理器后,配置文件中的<spring>节点才能起
  作用。(configSections必须是configuration下的第一个元素
  否则会编译出错。)
  context:容器资源列表(不能少,少了会出错)
  objects:容器里面的对象列表(不能少,少了会出错)
  -->
  <configSections>
    <sectionGroup name="spring">
      <!--WebContextHandler是在Web项目中使用的-->
      <!--section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/-->
      <!--ContextHandler是在Web以外的项目中使用的-->
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
    </sectionGroup>
  </configSections>
c)        app.config应用程序配置文件中,加入contextobjects对象的相关配置,加入的配置代码(注意,可以在上面的配置后加入下面的代码的):

  <!--
  SP2:配置Spring的容器,这样配置就不用在程序中显式地去创建
  Spring的容器,从而降低了程序对Spring的耦合。
  <context>节点的type属性是可选的,在Windows应用中,其默认值就是Spring.Context.Support.XmlApplicationContext
  -->
  <spring>
    <context>
      <!--SP3: 此处的配置文件是指包括了Spring.NET对象定义的XML文件,而非特指.config文件 -->
      <resource uri="config://spring/objects"/>
      <!--下面是引用.NET程序集内嵌资源时的URI语法:
      assembly://<AssemblyName>/<NameSpace>/<ResourceName>
      assembly://<程序集>/<命名空间>/<资源名称>
      SP_Manual:加入不同项目的不同xml配置信息。如:
      例:<resource uri="assembly://Piggy.NET.Web/Piggy.NET.Web/WebTest.xml"/>
      -->
      <resource uri="assembly://piggyWinForm/piggyWinFormTemplet/Objects.xml"/>
    </context>
    <!-- SP4:objects节点的xmlns元素是必需的,必须根据不同的应用添加不同的命名空间 -->
    <objects xmlns="http://www.springframework.net"/>
  </spring>
 d)piggyWinForm程序集中加入Objects.xml,其代码如下(该文件配置的是告知spring容器,哪个对象所使用的是什么程序集中的哪个类):

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

<objects xmlns="http://www.springframework.net"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.net
        http://www.springframework.net/xsd/spring-objects.xsd">

  <object id="SpringTest" type="piggyWinFormTemplet.SpringTest, piggyWinForm" />

</objects>
配置工作已经完成了,接下来,我贴出我封装了的Spring容器的类,方便我们在日后的使用。代码如下:

using Spring.Context;
using Spring.Context.Support;

namespace Piggy.Common
{
    public class SpringTool
    {
        private static IApplicationContext m_context = null;

        private SpringTool()
        {
        }

        public static void Close()
        {
            if (m_context != null)
            {
                AbstractApplicationContext c = (m_context as AbstractApplicationContext);
                if (c != null)
                    c.Dispose();
                m_context = null;
            }
        }

        public static IApplicationContext Context
        {
            get
            {
                if (m_context == null)
                {
                    lock (typeof(SpringTool))
                        m_context = ContextRegistry.GetContext();
                }
                return m_context;
            }
        }
    }
}
这样,我们就能方便地在其它地方使用,使用的方式如:object myObject=SpringTool.Context.GetObject("SpringTest"),注意这里的字符串参数,SpringTest是Objects.xml中的那个object id的值。

源码下载

原创作品出自努力偷懒,转载请说明文章出处http://blog.csdn.net/kfarvid或 http://www.cnblogs.com/kfarvid/




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值