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/

posted @   努力偷懒  阅读(829)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示