IOC实现:

IOC容器我们只停留在知道上是不行的,我们要动手做印象对更深刻,那么我给大家看一个代码。看看代码中IOC容器的实现。

代码实现:

创建一个类库:

解决方式的类库建立:

创建一个实体类:User:

<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Text; namespace Spring.Demo.Model
{
/// <summary>
/// 用户类
/// </summary>
public class Users
{
/// <summary>
/// 编号
/// </summary>
private int _oid;
public int Oid
{
get { return _oid; }
set { _oid = value; }
} /// <summary>
/// 姓名
/// </summary>
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
} /// <summary>
/// 性别
/// </summary>
private string _sex;
public string Sex
{
get { return _sex; }
set { _sex = value; }
} /// <summary>
/// 年龄
/// </summary>
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
}
}</span>

创建IUsers的接口:

<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Text;
namespace Spring.Demo.Service
{
public interface IUsers
{
/// <summary>
/// 返回用户的具体信息的方法
/// </summary>
/// <returns></returns>
string GetUserInfo();
}
}
</span>

创建一个实现IUsers接口的实现类:

<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Text;
using Spring.Demo.Service;
using Spring.Demo.Model; namespace Spring.Demo.Compontext
{
public class UsersCompontents : IUsers
{
public UsersCompontents()
{ } #region 获取用户信息
public string GetUserInfo()
{
Users user = new Users();
user.Oid = 1;
user.Name = "Beniao";
user.Sex = "Boy";
user.Age = 25; return string.Format("编号:{0}--->姓名:{1}--->性别:{2}--->年龄:{3}",
user.Oid,
user.Name,
user.Sex,
user.Age);
}
#endregion
}
}</span>

创建測试类:

<span style="font-size:18px;">using ITOO.Library.Core.AOP;
using Spring.Context;
using Spring.Demo.Service;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text; namespace Sping.Demo.SimpleTest
{
class Program
{
static void Main(string[] args)
{ IUsers studentChangeBll = SpringHelper.GetObject<IUsers>("Users"); Console.WriteLine(studentChangeBll.GetUserInfo());
Console.Read();
}
}
}</span>

在控制台程序中创建一个配置文件:

<span style="font-size:18px;"><?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"/>
</context>
<objects xmlns="http://www.springframework.net">
<!--这的配置依据实际的程序来的。UsersCompontents是程序集Spring.Demo.Compontext下的一个类-->
<object name="Users"
type="Spring.Demo.Compontext.UsersCompontents,Spring.Demo.Compontext" singleton="false" >
</object>
</objects>
</spring>
</configuration></span>

执行后,发现SpringHelper却小引用。

我们一般写代码中我们是这样写的:

<span style="font-size:18px;">//从config文件中取得程序集信息
IApplicationContext context = ConfigurationManager.GetSection("spring/context")
as IApplicationContext;
//调用方法
//Users为config文件中的配置节
//<object name="Users"
// type="Spring.Demo.Compontext.UsersCompontents,Spring.Demo.Compontent">
//</object>
IUsers user = context.GetObject("Users") as IUsers;</span>

这样我们就能够从配置文件里将对象取出来,可是我们都不想在代码中有多余的代码。不能每一次new对象的时候,我们都要写一遍这句话:IApplicationContext context = ConfigurationManager.GetSection("spring/context") as IApplicationContext;这样就添加了我们维护代码的成本,因此。我们将这句话封装起来,封装的代码是这种:

创建一个类:SpringHelper:

<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Context;
using Spring.Context.Support; namespace ITOO.Library.Core.AOP
{
    public class SpringHelper
    {
        /// <summary>
        /// Spring容器上下文
        /// </summary>
        private static IApplicationContext SpringContext
        {
            get
            {
                return ContextRegistry.GetContext();
            }
        }         /// <summary>
        /// 获取配置文件 配置的 对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="objName"></param>
        /// <returns></returns>
        public static T GetObject<T>(string objName) where T : class
        {
            return (T)SpringContext.GetObject(objName);
        }
    }
} </span>

以上的代码我们就能够将每次读取配置文件里的那句话去掉了,我们直接就能够写这样一句话就能够了:IUsers studentChangeBll = SpringHelper.GetObject<IUsers>("Users");

这里体现了封装的重要性。先前我在做AOP的时候。我的师傅看到了类似这种代码的时候,他就跟我讨论过这个问题,我当时懵懵懂懂,没有进行下一步的行动,如今想想,问题出如今我根本没有动手去做,或者知识没有深入到那个层次,认识这个知识的方面没有那么深。

全部问题,都要动手去做才行。

总结:

我们从上面的实践到分析之后,我们发现事实上我们看似是新的东西,事实上我们已经学习过了,就像IOC容器一样,我们学习过了反射和配置文件。我们发现事实上IOC容器不就是反射和配置文件来实现的吗,反射和配置文件是我们在大话设计模式中就已经学习到了的东西,这都不是新的东西。一个看似复杂的东西,都是有简单的东西来组装成的。我们知道这个。就不会对新的东西有畏惧感了。

反射 + 配置文件 实现IOC容器的更多相关文章

  1. 反射机制与IOC容器

    原文地址:http://blog.csdn.net/u010926964/article/details/47262771

  2. IOC容器特性注入第一篇:程序集反射查找

    学习kooboo的框架发现它的注入容器方法比较特别,同样是利用MVC的注入点,但它是查找网站下面bin所有的DLL利用反射查找特性找到对应的服务注入到容器. 这样的好处很简单:完全可以不用关心IOC容 ...

  3. 【spring源码学习】spring的IOC容器之自定义xml配置标签扩展namspaceHandler向IOC容器中注册bean

    [spring以及第三方jar的案例]在spring中的aop相关配置的标签,线程池相关配置的标签,都是基于该种方式实现的.包括dubbo的配置标签都是基于该方式实现的.[一]原理 ===>sp ...

  4. 【Spring Framework】Spring入门教程(四)注册Bean到IOC容器

    注册Bean到IOC容器大致分为4种: ①.包扫描+组件注解(@Controller.@Service.@Repository.@Component) 针对类是我们自己编写的情况 ②.@Bean注解 ...

  5. IoC原理-使用反射/Emit来实现一个最简单的IoC容器

    从Unity到Spring.Net,到Ninject,几年来陆陆续续用过几个IoC框架.虽然会用,但也没有一直仔细的研究过IoC实现的过程.最近花了点时间,下了Ninject的源码,研究了一番,颇有收 ...

  6. (反射+内省机制的运用)简单模拟spring IoC容器的操作

    简单模拟spring IoC容器的操作[管理对象的创建.管理对象的依赖关系,例如属性设置] 实体类Hello package com.shan.hello; public class Hello { ...

  7. IOC容器Unity的使用及独立配置文件Unity.Config

    [本段摘录自:IOC容器Unity 使用http://blog.csdn.net/gdjlc/article/details/8695266] 面向接口实现有很多好处,可以提供不同灵活的子类实现,增加 ...

  8. 第三节:工厂+反射+配置文件(手写IOC)对缓存进行管理。

    一. 章前小节 在前面的两个章节,我们运用依赖倒置原则,分别对 System.Web.Caching.Cache和 System.Runtime.Cacheing两类缓存进行了封装,并形成了ICach ...

  9. .net中反射与IOC容器实现

    反射还是很有用的,比如IOC容器基本上都是通过反射实现的. IOC是什么 IOC:Inversion of Control 控制反转是一种是面向对象编程中的一种设计原则,用来减低计算机代码之间的耦合度 ...

随机推荐

  1. unity3d教程运行物理机制

    首先,我们将把Hooke定律写Euler方法结合在一起找到新坐标.加速和速度. Hooke定律是F=kx,这里的F是指由水流产生的力(记住,我们将把水体表面模拟为水流),k是指水流的常量.x则是位移. ...

  2. .NET开源的背后:是无奈,还是顺应潮流?

    摘要:微软.NET的开源,让很多开发者欣喜若狂.同一时候也有很多人好奇其背后的故事,过去视开源为癌症的微软为什么会突然有这一举措,是出于无奈,还是顺应潮流,而这当中的种种也许能够用文中的六个观点来说明 ...

  3. 又一次发现Oracle太美之glogin.sql

    又一次发现Oracle太美之glogin.sql 刚開始接触Oracle的时候,有时候一登陆一个生产环境.常常会出现以下的情况: [oracle@rh64 app]$ sqlplus / as sys ...

  4. 15.Linux的文件结构

    linux的文件结构和windows不同,没有分区,是树形的结构: /etc:存放配置文件 /lib:编译程序需要的函数库 /usr:包含所有其他内容,比如内核在/usr/src中,/usr/bin存 ...

  5. 微信开发中的序列化json问题..

    微信开发平台: https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list& ...

  6. $.ajax 和$.post的区别

    https://blog.csdn.net/weixin_39709686/article/details/78680754

  7. html 编码与解码

    var HtmlUtil = { /*1.用浏览器内部转换器实现html转码*/ htmlEncode:function (html){ //1.首先动态创建一个容器标签元素,如DIV var tem ...

  8. Generic programming-泛型编程

    Generic programming is a style of computer programming in which algorithms are written in terms of t ...

  9. CDR中如何将对象在页面居中显示

    利用CorelDRAW在做设计排版时,如果想让对象在页面居中显示你会用什么方法?用鼠标拖?还是更准确的做法选择参照物对象,利用对齐与分布命令?或者还有更简单快速的方法,一起来看看吧! 最简单的方法(页 ...

  10. hibernate---crateria

    Leslie 趁还没忘掉,赶快记录下来 Hibernate中Criteria的完整用法 转自:http://www.360doc.com/content/090313/10/26262_2794855 ...