前面几篇文章分别介绍:程序集反射查找,特性,容器,但它们之间贯穿起来,形成查找Attribute注入IOC容器,就得需要下面这个类帮忙:

1.DependencyAttributeRegistrator(依赖特性注入类),有它才能在引擎初始化的时候 查找Attribute 进行注入

    public class DependencyAttributeRegistrator {
#region Fields private readonly ITypeFinder _finder;
private readonly IContainerManger _containerManger; #endregion #region .ctor public DependencyAttributeRegistrator(ITypeFinder finder, IContainerManger containerManger)
{
this._finder = finder;
this._containerManger = containerManger;
}
#endregion #region Methods public virtual IEnumerable<AttributeInfo<DependencyAttribute>> FindServices()
{
foreach (Type type in _finder.FindClassesOfType<object>())
{
var attributes = type.GetCustomAttributes(typeof (DependencyAttribute), false); //这句是关键 查找所有特性
foreach (DependencyAttribute attribute in attributes)
{
yield return new AttributeInfo<DependencyAttribute>
{
Attribute = attribute,
DecorateType = type
};
}
}
}
public virtual void RegisterServices() {
this.RegisterServices(this.FindServices());
}
public virtual void RegisterServices(IEnumerable<AttributeInfo<DependencyAttribute>> services)
{
services = services.OrderBy(it => it.Attribute.Order);
foreach (var info in services)
{
Type serviceType = info.Attribute.ServiceType ?? info.DecorateType;
_containerManger.AddComponent(serviceType,info.DecorateType,info.Attribute.Key,info.Attribute.LiftStyle);
}
} public virtual IEnumerable<AttributeInfo<DependencyAttribute>> FilterServices(
IEnumerable<AttributeInfo<DependencyAttribute>> services, params string[] configurationKeys)
{
return services.Where(s => s.Attribute.Configuration == null || configurationKeys.Contains(s.Attribute.Configuration));
}
#endregion
}

下一篇:

IOC容器特性注入第六篇:利用MVC注入点,把容器启动

IOC容器特性注入第五篇:查找(Attribute)特性注入的更多相关文章

  1. IOC容器特性注入第三篇:Attribute封装

    Attribute(特性)=>就是对类,方法,字段的自定义属性的基类.可以利用Attribute对类,方法等进行自定义描述,方便区分. 既然如此,那我们就可以那些需要注入IOC容器和不需要注入I ...

  2. IoC容器-Bean管理XML方式(创建对象和set注入属性,有参构造注入属性)

    Ioc操作Bean管理 1,什么是Bean管理 (0)Bean管理指的是两个操作 (1)Spring创建对象 (2)Spring注入属性 2,Bean管理操作有两种方式 (1)基于xml配置文件方式实 ...

  3. IoC容器-Bean管理XML方式(p名称空间注入)

    5,p名称空间注入(简化xml配置) (1)使用p名称空间注入,可以简化基于xml配置方式 (了解实际用不多) 第一步 添加 p 名称空间在配置文件中   第二步 进行属性注入,在bean标签里面进行 ...

  4. web安全之SQL注入---第五章 如何预防SQL注入 ?

    5-1严格检查输入变量的类型和格式总结:其实就是做一些判断正则表达式:验证密码:/^[a-zA-Z]{6,}$/5-1严格检查输入变量的类型和格式总结:其实就是做一些判断正则表达式:验证密码:/^[a ...

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

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

  6. IOC容器特性注入第二篇:初始引擎查找对应的IOC容器

    上篇文章介绍了如何利用反射类查找网站bin文件夹下面所有DLL的程序集类,这篇文章将介绍如何初始化一个IOC容器引擎. 目前IOC容器有很多,如Ninject,Autofac等,每个容器的驱动都不一样 ...

  7. IOC容器特性注入第四篇:容器初始化

    IOC容器,就是把各种服务都注入到容器里,想要什么就去拿什么,不仅解决服务类和接口类的耦合度还可以提高性能方便管理. 这里封装一个接口类和一个实现类 1.IContainerManager接口 pub ...

  8. SpringBoot启动流程分析(六):IoC容器依赖注入

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  9. Spring注解驱动开发之Ioc容器篇

    前言:现今SpringBoot.SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解.原理,比如@Conditional.@Import.@Ena ...

随机推荐

  1. ClassLoader加载资源时的搜索路径

    先来个例子: /** * 测试classloader加载路径在哪里<p> * main3 */ public static void main3(String[] args) { Prop ...

  2. php中__call() 和 __callStatic方法的使用

    __call 与__callStatic 魔法方法是php5.3后新增的,二者的应用场景: 1.当要调用的方法不存在或权限不足时,会自动调用__call 方法. 2.当调用的静态方法不存在或权限不足时 ...

  3. PID控制器(比例-积分-微分控制器)- I

    形象解释PID算法 小明接到这样一个任务: 有一个水缸点漏水(而且漏水的速度还不一定固定不变),要求水面高度维持在某个位置,一旦发现水面高度低于要求位置,就要往水缸里加水. 小明接到任务后就一直守在水 ...

  4. wordpress模板修改及函数说明

    原文:http://java-er.com/blog/wp-mb-edit/ WordPress基本模板文件 style.css : CSS(样式表)文件index.php : 主页模板archive ...

  5. git 对比两个commit 之间的差异

    git 对比两个commit 之间的差异 比较两个版本之间的差异 git diff commit-id-1 commit-id-2 > d:/diff.txt 结果文件diff.txt中: &q ...

  6. oracle11g-linux 归档处理

    在使用oracle时突然登录不上去了提示:ORA-00257:archiver error.Connect internal only,until freed. 查遍很多资料,都指向“归档日志空间不足 ...

  7. 阮一峰的js教程,值得一读

    http://javascript.ruanyifeng.com/introduction/intro.html

  8. Using std::map with a custom class key

    From: https://www.walletfox.com/course/mapwithcustomclasskey.php If you have ever tried to use a cus ...

  9. IDA反汇编学习

    1 转自:http://www.cnblogs.com/vento/archive/2013/02/09/2909579.html IDA Pro是一款强大的反汇编软件,特有的IDA视图和交叉引用,可 ...

  10. ELK菜鸟手记 (一) 环境配置+log4j日志记录

    1. 背景介绍 在大数据时代,日志记录和管理变得尤为重要. 以往的文件记录日志的形式,既查询起来又不方便,又造成日志在服务器上分散存储,管理起来相当麻烦, 想根据一个关键字查询日志中某个关键信息相当困 ...