反射 + 配置文件 实现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 控制反转是一种是面向对象编程中的一种设计原则,用来减低计算机代码之间的耦合度 ...
随机推荐
- 32.QT绘图
widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QPainter> #inclu ...
- 8.QList QMap QVariant
QList int main1(int argc, char *argv[]) { QApplication a(argc, argv); QList<,,}; mylist << ...
- tableview偏移
tableview偏移 方法一:改变edgesForExtendedLayout self.edgesForExtendedLayout = UIRectEdgeNone; 将edgesForExte ...
- [makefile]如何设置不同目录的代码(.c),生成到指定目录下(./debug/.o))
部分代码跟makefile不在同一目录,有没有好的方法来设置依赖关系,我找到三种方法,但感觉都不完美,下面我会把他列出来并加以说明,不知有没有更好的方法,makefile本身也不是很熟,请大家指教: ...
- java中拦截器 过滤器 监听器都有什么区别
过滤器,是在java web中,你传入的request,response提前过滤掉一些信息,或者提前设置一些参数,然后再传入servlet或者struts2的action进行业务逻辑,比如过滤掉非法u ...
- jsoup HTML parser hello world examples--转
原文地址:http://www.mkyong.com/java/jsoup-html-parser-hello-world-examples/ Jsoup, a HTML parser, its “j ...
- 软件测试作业-selenium
一.作业要求 1.安装SeleniumIDE插件. 2.学会使用SeleniumIDE录制脚本和导出脚本. 3.访问网址使用学号登录系统,进入系统后可以看到该同学的git地址. 4.编写Seleniu ...
- Android Finalizing a Cursor that has not been deactivated or closed
问题描述: 使用Sqlite数据库时,有时候会报下面的异常: Finalizing a Cursor that has not been deactivated or closed 一个光标没有被停用 ...
- luoguP4512 【模板】多项式除法 NTT+多项式求逆+多项式除法
Code: #include<bits/stdc++.h> #define maxn 300000 #define ll long long #define MOD 998244353 # ...
- Pyhton学习——Day11
# Python中的内部模块# 函数学习的意义:抽取重复代码# 模块:不用重复写,模块及py文件,提高了代码的可维护性,其次,编写代码不必从零开始,当一个模块编写完毕,不必再重复编写# import ...