IOC容器特性注入第五篇:查找(Attribute)特性注入
前面几篇文章分别介绍:程序集反射查找,特性,容器,但它们之间贯穿起来,形成查找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容器特性注入第五篇:查找(Attribute)特性注入的更多相关文章
- IOC容器特性注入第三篇:Attribute封装
Attribute(特性)=>就是对类,方法,字段的自定义属性的基类.可以利用Attribute对类,方法等进行自定义描述,方便区分. 既然如此,那我们就可以那些需要注入IOC容器和不需要注入I ...
- IoC容器-Bean管理XML方式(创建对象和set注入属性,有参构造注入属性)
Ioc操作Bean管理 1,什么是Bean管理 (0)Bean管理指的是两个操作 (1)Spring创建对象 (2)Spring注入属性 2,Bean管理操作有两种方式 (1)基于xml配置文件方式实 ...
- IoC容器-Bean管理XML方式(p名称空间注入)
5,p名称空间注入(简化xml配置) (1)使用p名称空间注入,可以简化基于xml配置方式 (了解实际用不多) 第一步 添加 p 名称空间在配置文件中 第二步 进行属性注入,在bean标签里面进行 ...
- web安全之SQL注入---第五章 如何预防SQL注入 ?
5-1严格检查输入变量的类型和格式总结:其实就是做一些判断正则表达式:验证密码:/^[a-zA-Z]{6,}$/5-1严格检查输入变量的类型和格式总结:其实就是做一些判断正则表达式:验证密码:/^[a ...
- IOC容器特性注入第一篇:程序集反射查找
学习kooboo的框架发现它的注入容器方法比较特别,同样是利用MVC的注入点,但它是查找网站下面bin所有的DLL利用反射查找特性找到对应的服务注入到容器. 这样的好处很简单:完全可以不用关心IOC容 ...
- IOC容器特性注入第二篇:初始引擎查找对应的IOC容器
上篇文章介绍了如何利用反射类查找网站bin文件夹下面所有DLL的程序集类,这篇文章将介绍如何初始化一个IOC容器引擎. 目前IOC容器有很多,如Ninject,Autofac等,每个容器的驱动都不一样 ...
- IOC容器特性注入第四篇:容器初始化
IOC容器,就是把各种服务都注入到容器里,想要什么就去拿什么,不仅解决服务类和接口类的耦合度还可以提高性能方便管理. 这里封装一个接口类和一个实现类 1.IContainerManager接口 pub ...
- SpringBoot启动流程分析(六):IoC容器依赖注入
SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...
- Spring注解驱动开发之Ioc容器篇
前言:现今SpringBoot.SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解.原理,比如@Conditional.@Import.@Ena ...
随机推荐
- 我定制的Stylish样式
没有办法上传,只好存到这里. 天涯 .atl-con-ft, .js-zhiyin-area, .ds_seat_foot, .hongbao-btn, .dashang-left-btn, .qrc ...
- linux 校准时间
ntpdate cn.pool.ntp.org //查看硬件时间可以是用hwclock,hwclock --show 或者hwclock -r [root@localhost ~]# hwclock ...
- weblogic清除缓存
背景:在开发调试或测试时,很多时候重新更新部署服务后会发现某些更新并没有体现到,还是之前的情况,也或者会出现其他错误问题,这个时候就要考虑到可能是weblogic缓存未清理引起. 可以先尝试下面这种做 ...
- Redis简单延时队列
Redis实现简单延队列, 利用zset有序的数据结构, score设置为延时的时间戳. 实现思路: 1.使用命令 [zrangebyscore keyName socreMin socreMax] ...
- C# DES (ECB模式) 加密解密 --单倍长
加密: 调用时: Encrypt_DES16("2AF349243535BCD3", "1111111111111111"); public static s ...
- Java8 利用Lambda处理List集合循环给另外一个List赋值过滤处理
1.利用stream().forEach()循环处理List; List<String> list = Lists.newArrayList();//新建一个List 用的google提供 ...
- shell中函数返回值
1.前言 快半年没有写博客了,荒废了很久,工作中的杂事太多,自己越来越懒了.为了鞭策自己成长,还是要坚持写写博客,记录自己的成长. 2.shell函数介绍 语法: [ function ] funna ...
- 【T10】记住,TCP__IP不是轮询的
1.TCP/IP协议栈无法将连接的丢失立即通知应用程序. TCP为什么不提供这种通知机制,不这么做的优点和缺点,应用程序怎么检测链接的丢失. 2.TCP为什么不提供连接丢失即时通知的功能? a.其他通 ...
- Android性能优化-减小图片下载大小
原文链接 https://developer.android.com/topic/performance/network-xfer.html 内容概要 理解图片的格式 PNG JPG WebP 如何选 ...
- Python 读取WAV文件并绘制波形图
aa Python 读取WAV文件并绘制波形图 ffmpeg -i test_pcm_mulaw.wav -f wav -codec:a pcm_s16le -ar 8000 -ac 1 out.wa ...