码迷,mamicode.com
首页 > 编程语言 > 详细

Spring.Net学习笔记(一)-容器的使用

时间:2016-02-21 22:32:38      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:

一、下载地址:

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

二、相关程序集

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

Spring.NET-1.3.1\Spring.NET\bin\net\4.0\release目录下

三、创建容器

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); 
    }
}

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);
    }
}

4.使用配置文件

需要使用一个新的配置处理器: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();
    }
}

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>

Spring.Net学习笔记(一)-容器的使用

标签:

原文地址:http://www.cnblogs.com/2star/p/5205628.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!