前面几篇文章分别介绍:程序集反射查找,特性,容器,但它们之间贯穿起来,形成查找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. spring源码分析系列 (1) spring拓展接口BeanFactoryPostProcessor、BeanDefinitionRegistryPostProcessor

    更多文章点击--spring源码分析系列 主要分析内容: 一.BeanFactoryPostProcessor.BeanDefinitionRegistryPostProcessor简述与demo示例 ...

  2. grep搜索文本

    正则匹配: grep -E "[a-z]+" 只输出匹配到的文本: echo this is a line. | grep -o -E "[a-z]+\." 统 ...

  3. UEditor实现前后端分离时单图上传

    首先,需要下载部署ueditor相关代码,可以参考一篇简单的博客,这里不再赘述: 环境搭建好后,我们先简单使用一下,启动http://web.yucong.com:8080/ueditor/index ...

  4. Detour3.0 win7 64bit下的安装

    最近在做API hook相关的东西,用了inline hook后感觉不错,但是查找资料发现inline hook并不稳定 inline hook 的原理是在系统访问一个函数的时候先替换原函数入口处的内 ...

  5. python3 requests获取某网站折线图上数据

    比如要抓取某网站折线图上数据,如下截图: 借助Chrome开发者工具Network.经过分析发现获取上面的热度数据,找到对应的事件url:https://pcw-api.iqiyi.com/video ...

  6. Android -- 再来一发Notification

    之前写过一个Notificaiton的文章,用上面的方式去操作也是OK的,但是到后面的SDK之后,有些方法被弃用,甚至我到SDK23的时候,我发现有些方法直接没了,所以在这里重新写一下最新的用法. h ...

  7. shell编程学习笔记(二):Shell中变量的使用

    变量在很多编程语言中都有,Shell中也不例外,我们下面看一下Shell中的变量怎么使用: 以下蓝色字体部分为Linux命令,红色字体的内容为输出的内容: # cd /opt/scripts # vi ...

  8. Linux-文件描述符的本质及与文件指针的区别

    文章参考:文件描述符的本质.文件描述符和文件指针的区别.文件描述符fd和文件指针flip的理解 推荐:task_struct 和文件系统的关系 系统中文件相关表 右侧的表称为i节点表,在整个系统中只有 ...

  9. JAVA Spring boot相关技巧

    1. 注册多实例.@Scope("prototype") 2. 手工方式获取注册的实例. @Autowired private ServletContext servletCont ...

  10. Apache学习---多进程处理模块(MPM)原理详解

    查看Apache的模式,可以使用httpd -V命令来查看: 1. prefork MPM prefork模式可以算是很古老但是非常稳定的Apache模式.Apache在启动之初,就预先fork一些子 ...