反射 + 配置文件 实现IOC容器
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容器的更多相关文章
- 反射机制与IOC容器
原文地址:http://blog.csdn.net/u010926964/article/details/47262771
- IOC容器特性注入第一篇:程序集反射查找
学习kooboo的框架发现它的注入容器方法比较特别,同样是利用MVC的注入点,但它是查找网站下面bin所有的DLL利用反射查找特性找到对应的服务注入到容器. 这样的好处很简单:完全可以不用关心IOC容 ...
- 【spring源码学习】spring的IOC容器之自定义xml配置标签扩展namspaceHandler向IOC容器中注册bean
[spring以及第三方jar的案例]在spring中的aop相关配置的标签,线程池相关配置的标签,都是基于该种方式实现的.包括dubbo的配置标签都是基于该方式实现的.[一]原理 ===>sp ...
- 【Spring Framework】Spring入门教程(四)注册Bean到IOC容器
注册Bean到IOC容器大致分为4种: ①.包扫描+组件注解(@Controller.@Service.@Repository.@Component) 针对类是我们自己编写的情况 ②.@Bean注解 ...
- IoC原理-使用反射/Emit来实现一个最简单的IoC容器
从Unity到Spring.Net,到Ninject,几年来陆陆续续用过几个IoC框架.虽然会用,但也没有一直仔细的研究过IoC实现的过程.最近花了点时间,下了Ninject的源码,研究了一番,颇有收 ...
- (反射+内省机制的运用)简单模拟spring IoC容器的操作
简单模拟spring IoC容器的操作[管理对象的创建.管理对象的依赖关系,例如属性设置] 实体类Hello package com.shan.hello; public class Hello { ...
- IOC容器Unity的使用及独立配置文件Unity.Config
[本段摘录自:IOC容器Unity 使用http://blog.csdn.net/gdjlc/article/details/8695266] 面向接口实现有很多好处,可以提供不同灵活的子类实现,增加 ...
- 第三节:工厂+反射+配置文件(手写IOC)对缓存进行管理。
一. 章前小节 在前面的两个章节,我们运用依赖倒置原则,分别对 System.Web.Caching.Cache和 System.Runtime.Cacheing两类缓存进行了封装,并形成了ICach ...
- .net中反射与IOC容器实现
反射还是很有用的,比如IOC容器基本上都是通过反射实现的. IOC是什么 IOC:Inversion of Control 控制反转是一种是面向对象编程中的一种设计原则,用来减低计算机代码之间的耦合度 ...
随机推荐
- caffe-ssd使用预训练模型做目标检测
首先参考https://www.jianshu.com/p/4eaedaeafcb4 这是一个傻瓜似的目标检测样例,目前还不清楚图片怎么转换,怎么验证,后续继续跟进 模型测试(1)图片数据集上测试 p ...
- MySQL8.0修改临时密码
解决MySQL8.0报错:Unknown system variable 'validate_password_policy' 一.问题描述 1.在安装MySQL8.0时,修改临时密码,因密码过于简单 ...
- 8. String to Integer[M]字符串转整数
题目 Inplement atoi which converts a string to an integer. The function first discards as many whitesp ...
- python之--初始面向对象
阅读目录 楔子 面向过程vs面向对象 初识面向对象 类的相关知识 对象的相关知识 对象之间的交互 类命名空间与对象.实例的命名空间 类的组合用法 初识面向对象小结 面向对象的三大特性 继承 多态 封装 ...
- avformat_find_stream_info函数卡住问题
问题:初始化RTSP流时,在android设备上卡住在avformat_find_stream_info函数,然后程序崩溃. 但其他URL没问题,且同样在代码在iOS上没问题,由于jni调试,也没看到 ...
- Redis运维时需要注意的参数
1: 内存 Memory used_memory:859192 数据结构的空间 used_memory_rss:7634944 实占空间 mem_fragmentation_ratio:8.89 前2 ...
- elment表格分页
项目的时候遇到了一个分页的bug,经过分析Element源码之后找到了问题所在,现在把这个问题及解决方法记录下来. 项目中要实现的功能是用户选择查看表格的时候在任意页面点击查询,得到结果之后要展示的页 ...
- [BOI2011]MET-Meteors
题目:洛谷P3527. 题目大意:n个国家在某星球上建立了m个空间站(一个空间站只属于一个国家),空间站围成一个环.现在知道要下k天陨石,每天都在一个区间内下,每个点都下同样多的(若r>l,则说 ...
- 升级ruby到2.0
本文部分内容转载,如侵犯个人利益请联系博客管理员及时删除,或留言之评论区 一.安装库 Yum install –y gcc* openssl* wget 二.安装ruby wget https://c ...
- Python中的itertools.product
例子1:import itertoolsa = itertools.product([1,2,3],[100,200])print(a)for item in itertools.product([1 ...