zoukankan      html  css  js  c++  java
  • 『Spring.Net』IoC 容器

     

    什么是IoC


     image

    如何使用Spring.NET的IoC功能


    翻译一段文档中的原话做为IoC使用的开始,由于水平所限,是否完全正确,就不要过于深究的……

    The Spring.Core assembly is the basis for Spring.NET's IoC container. The IObjectFactory interface provides an advanced configuration mechanism capable of managing any type of object. IApplicationContext is a sub-interface of IObjectFactory. It adds easier integration with Spring.NET's Aspect Oriented Programming (AOP) features, message resource handling (for use in internationalization), event propagation and application layer-specific context such as WebApplicationContext for use in web applications.

    Spring.Core提供的基础的Spring.NET IoC 容器的功能。IObjectFactory接口提供一种能够管理任何类型对象的高级的配置机制。IApplicationContext是IObjectFactory的一个子接口。它增加便于整合Spring.NET 的AOP特性、信息资源处理(用于国际化),事件的传播和应用于应用程序,就像WebApplicationContext用于网络应用程序。

    这段话中告诉我们两点最有需要知道的:

    1. 要引用 Spring.Core
    2. 要在代码中使用 IObjectFactory 的某个子项

    使用步骤:

    1. 引用 Spring.Core
    2. 建立配置文件(可以写在App.config中)
    3. 将配置文件读入程序
    4. 使用GetObject方法完成实例化过程
    5. 使用实例

     

    • id : 这里我把它看成是类的映射名称。因为在IoC中主要是使用反射的机制来实现的,所以这里可以看成是那个提供给反射的字符串。
    • type :  这里有两项,第一项是真正用于注入的那个「类」,第二项是程序集名称。

    • 括号中可以是一个配置文件,也可以是多个。个数的多少主要看实际的需要。
    • 配置文件的路径要是Debug文件夹中的。

    参考代码


    <objects xmlns="http://www.springframework.net">
    
        <!-- <import resource="services.xml"/> -->
        
        <object id="AccVipDao" type="newpos.dao.impl.emax.TAccVipDA, newpos.dao.impl.emax">
            <!-- collaborators and configuration for this object go here -->
        </object>
    
        <!-- more object definitions go here -->
    
    </objects>
    namespace newpos.test
    {
        class Program
        {
            static void Main(string[] args)
            {
                IApplicationContext context = new XmlApplicationContext("Objects.xml");
                IAccVipDao vip = (IAccVipDao)context.GetObject("AccVipDao");
    
                TAccVip vipInfo = vip.GetDataAllByVipNo("123456");
    
                Console.WriteLine(vipInfo.EnterDate);
                Console.ReadLine();
            }
        }
    }

    其他


    Property

    More info

    type Section 5.2.5, “Instantiating objects”
    id and name Section 5.2.4.1, “Naming objects”
    singleton or prototype Section 5.4, “Object Scopes”
    object properties Section 5.3.1, “Dependency injection”
    constructor arguments Section 5.3.1, “Dependency injection”
    autowiring mode Section 5.3.6, “Autowiring collaborators”
    dependency checking mode Section 5.3.7, “Checking for dependencies”
    initialization method Section 5.6.1, “Lifecycle interfaces”
    destruction method Section 5.6.1, “Lifecycle interfaces”
  • 相关阅读:
    [zhuanzai]Bean对象注入失败 .NoSuchBeanDefinitionException: No qualifying bean of type..
    Quartz框架介绍
    [转载]springboot--常用注解--@configration、@Bean
    [转载]ac mysql 无法远程连接
    【转载】总结:几种生成HTML格式测试报告的方法
    【转载】SELENIUM2支持无界面操作(HTMLUNIT和PHANTOMJS)
    Code Coverage for your Golang System Tests
    [转载]pytest学习笔记
    数据库系统概论-第一章
    数据库系统概论-目录篇
  • 原文地址:https://www.cnblogs.com/sitemanager/p/2334225.html
Copyright © 2011-2022 走看看