zoukankan      html  css  js  c++  java
  • 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://www.cnblogs.com/kfarvid/

  • 相关阅读:
    ThinkPHP 3.2.2 实现持久登录 ( 记住我 )
    Java实现 LeetCode 20 有效的括号
    Java实现 LeetCode 20 有效的括号
    Java实现 LeetCode 19删除链表的倒数第N个节点
    Java实现 LeetCode 19删除链表的倒数第N个节点
    Java实现 LeetCode 19删除链表的倒数第N个节点
    Java实现 LeetCode 18 四数之和
    Java实现 LeetCode 18 四数之和
    Java实现 LeetCode 18 四数之和
    Java实现 LeetCode 17 电话号码的字母组合
  • 原文地址:https://www.cnblogs.com/kfarvid/p/1826827.html
Copyright © 2011-2022 走看看