• Spring.NET依赖注入框架学习--实例化容器常用方法


    Spring.NET依赖注入框架学习---实例化容器常用方法

    本篇学习实例化Spring.NET容器的俩种方式

    1、通过XmlObjectFactory创建一个Spring.NET容器

    IResource input = new FileSystemResource ("objects.xml");
    IObjectFactory factory = new XmlObjectFactory(input);

    这样就可以通过factory的GetObject(“objectName”);获取这个对象

    2、通过IApplicationContext创建一个Spring.NET容器

    IApplicationContext ctx = ContextRegistry.GetContext();

    这样就可以通过IApplicationContext的GetObject(“objectName”);获取这个对象

    程序例子

    例子代码:Person.cs

    namespace Spring.NET01
    {
        public class Person
        {
            public Person()
            { }
            ~Person()
            { }
            public void print()
            {
                Console.WriteLine("我是一个Person对象");
            }
        }
    }

    App.config文件

    <?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="config://spring/objects"></resource>
        </context>
    
        <objects xmlns="http://www.springframework.net">
          
          <!--一个person对象-->
          <object id="person1" type="Spring.NET01.Person,Spring.NET01">
            
          </object>
        </objects>
    
      </spring>
    </configuration>

    添加objects.xml  其中objects 的属性值必须加上

    <?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="person2" type="Spring.NET01.Person,Spring.NET01">
      </object>
    </objects>

    测试代码:

     class Program
        {
            static void Main(string[] args)
            {
                //普通对象创建
                Console.WriteLine("--------普通对象创建方法--------");
                Person person = new Person();
                person.print();
    
                //通过Spring.NET ioc IApplicationContext注入对象
                Console.WriteLine("--------IApplicationContext方法--------");
                IApplicationContext content = ContextRegistry.GetContext();
                Person bennanhai = (Person)content.GetObject("person1");
                bennanhai.print();
    
    
                //通过Spring.NET ioc XmlObjectFactory注入对象
                Console.WriteLine("--------XmlObjectFactory方法--------");
                IResource input = new FileSystemResource("objects.xml");
                IObjectFactory factory = new XmlObjectFactory(input);
                Person bennanhai2 = (Person)factory.GetObject("person2");
                bennanhai2.print();
                Console.Read();
            }
        }

    运行结果

    源代码工程下载

  • 相关阅读:
    Qt绘图设备QPicture
    unique_lock加锁adopt_lock
    C++单例模式
    C++条件变量condition_variable
    unique_lock加锁try_to_lock
    unique_lock加锁defer_lock
    lock_guard加锁
    Linux快速入门(一)Linux基础知识
    Linux快速入门(四)Linux用户管理
    Linux快速入门(五)Linux系统管理
  • 原文地址:https://www.cnblogs.com/JiYF/p/8284637.html
Copyright © 2020-2023  润新知