zoukankan      html  css  js  c++  java
  • Spring.NET依赖注入(二) 对象的注入

    1. 抽象注入接口

    public interface IDeviceWriter
        {
            void saveToDevice();
        }

    2. 接口的具体实现

      实现1

    public class FloppyWriter : IDeviceWriter
        {
            public void saveToDevice() 
            {
                Console.WriteLine("储存至软盘…");
            }
        }

      实现2

    public class UsbDiskWriter : IDeviceWriter
        {
            public void saveToDevice() 
            {
                Console.WriteLine("储存至移动硬盘…");
            }
        }

    3. 需要注入的业务对象

    public class MemoryDevice
        {
            public IDeviceWriter DeviceWriter { getset; }

            public void Save()
            {
                if (DeviceWriter == null)
                {
                    throw new Exception("需要设备...");
                }

                DeviceWriter.saveToDevice();
            }
        }

    4. 业务调用组件

    public class DeviceComponent
        {
            public static void SaveDevice()
            {
                try
                {
                    //从config文件中取得程序集信息
                    IApplicationContext context = ConfigurationManager.GetSection("spring/context"as IApplicationContext;

                    //调用方法
                    MemoryDevice device = context.GetObject("algDevice"as MemoryDevice;
                    device.Save();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

    5. 客户端调用

    class SpringDeviceTest : ITestCase
        {
            public void Run()
            {
                DeviceComponent.SaveDevice();

                Console.Read();
            }
        }

    6. 配置文件

    <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"/>
        </context>
        
        <objects>
         ...
         <object id="algFloppy" type="CsharpTrainer.Strategy.Device.FloppyWriter, CsharpTrainer.Strategy" />
          <object id="algUsb" type="CsharpTrainer.Strategy.Device.UsbDiskWriter, CsharpTrainer.Strategy" />
          <object id="algDevice" type="CsharpTrainer.Strategy.Device.MemoryDevice, CsharpTrainer.Strategy">
            <property name="DeviceWriter" ref="algFloppy" />
            <!--<property name="DeviceWriter" ref="algUsb" />-->
          </object>
        </objects>
      </spring>
      ...
    </configuration>

    7. 运行结果

      储存至软盘…

    技术改变世界
  • 相关阅读:
    1300多万条数据30G论坛大数据优化实战经验小结(转)
    spring 攻略第二版文摘
    关于extjs中动态添加TabPanel的tab项并以iframe显示的整理(转)
    effective_java_2nd_endition文摘
    软件开发中常见的十大系统瓶颈(转)
    浅谈JAVA集合框架(转)
    java EE设计模式spring企业级开发最佳实践文摘
    layout 布局(转)
    COM高手总结的八个经验和教训
    IShellLink应用(创建快捷方式)
  • 原文地址:https://www.cnblogs.com/davidgu/p/2528024.html
Copyright © 2011-2022 走看看