zoukankan      html  css  js  c++  java
  • Spring.Net学习笔记(1)-容器的使用

    一、下载地址:

    http://www.springframework.net/download.html

    二、相关程序集

    Spring.Net容器定义在程序集Spring.Core.dll中,它依赖于Common.Logging.dll。该程序集位于

    Spring.NET-1.3.1Spring.NETin et4.0 elease目录下

    三、创建容器

    1.编程方式的容器

    使用Spring.Context.Support.StaticApplicationContxt直接创建容器

    public class Person
        {
            public string Name { get; set; }
            public override string ToString()
            {
                return "This is Person";
            }
        }
    class Program
    {
        static void Main(string[] args)
        {
            //创建容器
            Spring.Context.Support.StaticApplicationContext context = new StaticApplicationContext();
    
            //注册Person类
            context.RegisterPrototype("Person", typeof(Person), null);
    
            //从容器中获取Person对象
            Person person = context.GetObject("Person") as Person;
    
            Console.WriteLine(person);
        }
    }

    2.使用xml方式(注意xml的输出方式)

    处理xml的配置处理器:Spring.Context.Support.XmlApplicationContext

    <?xml version="1.0" encoding="utf-8" ?>
    <objects xmlns="http://www.springframework.net">
      <object id="Person" type="CPrj.Person,Cprj"></object>
    </objects>
    class Program
    {
        static void Main(string[] args)
        {
            Spring.Context.Support.XmlApplicationContext context = new XmlApplicationContext("objects.xml");
            Person person = context.GetObject("Person") as Person;
            Console.WriteLine(person); 
        }
    }

    注:<object>的父节点必须是<objects>,且objects的xmlns元素是必须的

    3.使用配置文件+xml文件

    处理器Spring.Context.Support.ContextHandler在启动程序时候加载配置信息

    <?xml version="1.0" encoding="utf-8" ?>
    <objects xmlns="http://www.springframework.net">
      <object id="Person" type="CPrj.Person,Cprj"></object>
    </objects>
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    
      <configSections>
        <sectionGroup name="spring">
          <section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"></section>
        </sectionGroup>
      </configSections>
    
      <spring>
        <context>
          <resource uri="file://objects.xml"></resource>
        </context>
      </spring>
    
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
    </configuration>
    class Program
    {
        static void Main(string[] args)
        {
            Spring.Context.IApplicationContext context = Spring.Context.Support.ContextRegistry.GetContext();
            Person person = context.GetObject("Person") as Person;
            Console.WriteLine(person);
        }
    }

    注:context节点的默认type就是 Spring.Context.Support.XmlApplicationContext,Spring.Core 。故可省去

    <spring>
      <context type="Spring.Context.Support.XmlApplicationContext,Spring.Core">
        <resource uri="file://objects.xml"></resource>
      </context>
    </spring>

    4.使用配置文件

    xml对象定义也可以放在DotNet的标准应用程序配置文件中,此时必须为<objects>节点预先注册相应的节点处理器

    需要使用一个新的配置处理器:Spring.Context.Support.DefaultSectionHandler

    它可以帮助我们解析spring配置信息

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    
      <configSections>
        <sectionGroup name="spring">
          <section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"></section>
          <section name="objects" type="Spring.Context.Support.DefaultSectionHandler,Spring.Core"/>
        </sectionGroup>
      </configSections>
    
      <spring>
        <context>
          <resource uri="config://spring/objects"></resource>
        </context>
        <objects xmlns="http://www.springframework.net">
          <object id="Person" type="CPrj.Person,CPrj"></object>
        </objects>
      </spring>
    
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
    </configuration>
    class Program
    {
        static void Main(string[] args)
        {
            Spring.Context.IApplicationContext context = Spring.Context.Support.ContextRegistry.GetContext();
            Person person = context.GetObject("Person") as Person;
            Console.WriteLine(person);
            Console.ReadKey();
        }
    }

    注:spring和context节点名称不是任意的,必须是spring和context。Spring.Net本身将“spring/context”作为字符串常亮定义在AbstractApplicationContext类中以表示上下文的节点名称

    image

    5.混合使用外部配置文件和嵌入的配置

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <sectionGroup name="spring">
                <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
                <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
            </sectionGroup>
        </configSections>
    
        <spring>
            <context>
                <resource uri="file://objects.xml"/>
                <resource uri="config://spring/objects"/>
            </context>
            <objects>
                <object id="Person" type="CPrj.Person,CPrj"></object>
            </objects>
        </spring>
        
    </configuration>

    四、参考文章

    http://www.cnblogs.com/haogj/archive/2011/06/10/2077540.html

    更多精彩内容请看:http://www.cnblogs.com/2star
  • 相关阅读:
    调用控制台程序函数 RunProcess
    url传递中文的解决方案(备忘)
    Microsoft.SharePoint.SPException 安全性验证无效——错误解决
    Windows SharePoint Services 虚拟服务器没被配置为与 ASP.NET 2.0.50727.42 一起使用解决办法
    automation服务器不能创建对象
    InfoPath窗体事件列表说明和示例使用
    ORA01113:文件n需要介质恢复 (转载)
    DevExpress 第三方控件汉化的全部代码和使用方法 (转载)
    爱的感觉(转载)
    关于Oracle 01122,01110,01207的错误和解决(转载)
  • 原文地址:https://www.cnblogs.com/kimisme/p/5205628.html
Copyright © 2011-2022 走看看