zoukankan      html  css  js  c++  java
  • 【Autofac打标签模式】AutoConfiguration和Bean

    支持的标签一览

    标签名称使用位置使用说明
    AutoConfiguration 打在class上面 自动装配class里面带有Bean标签的方法
    Bean 打在方法上面 配合AutoConfiguration标签使用
    Component 打在class上面 自动注册
    Autowired 打在构造方法的Parameter,类的Property,类的Field 自动装配
    PropertySource 打在class上面 配合Value标签使用,设置Value的数据源,支持json,xml,支持资源内嵌
    Value 打在构造方法的Parameter,类的Property,类的Field 静态数据装配
    Aspect 打在class上面 开启拦截器,默认注册为类拦截器,可以指定接口型拦截器

     

    【Autofac打标签模式】AutoConfiguration和Bean

     

    【 Autofac打标签模式】开源DI框架扩展地址:

    https://github.com/yuzd/Autofac.Annotation/wiki

    让一个配置类去注册一些单例对象到DI容器内

    • 打了AutoConfiguration标签的Class就是配置类
    • 在AutoConfiguration标签的Class里面打了Bean标签的返回对象就是要注册的单例对象

    image

    原理解释:

    框架会扫描打了【AutoConfiguration】的class,然后会再扫描打了【Bean】标签的方法并且Invoke该方法,拿到方法返回的对象并以单例的形式注册到DI容器中!

    AutoConfiguration标签支持下面几个参数配置

    image

    • Key:如果指定key
    • OrderIndex:值越大越先处理

    Key的使用方法请查看下面的实例代码的注释

    image

    
        //这个会加载
        [AutoConfiguration("Test")]
        public class MyConfig
        {
            [Bean]
            public MyModel GetMyModel()
            {
                return new MyModel
                {
                    Name = "yuzd"
                };
            }
        }
        
        //这个不会被加载
        [AutoConfiguration("Prod")]
        public class MyConfig2
        {
            [Bean]
            public MyModel GetMyModel()
            {
                return new MyModel
                {
                    Name = "yuzd"
                };
            }
        }
        
        // autofac打标签模式 并且调用了 SetAutofacConfigurationKey方法 指定了 Key  Test// 所有只会加载 打了且指定了Key=Test”的AutoConfiguration标签class
        builder.RegisterModule(new AutofacAnnotationModule(typeof(MyConfig).Assembly).SetAutofacConfigurationKey("Test"));
        
    
    
        //这个会加载
        [AutoConfiguration]
        public class MyConfig
        {
            [Bean]
            public MyModel GetMyModel()
            {
                return new MyModel
                {
                    Name = "yuzd"
                };
            }
        }
        
        //这个会被加载
        [AutoConfiguration]
        public class MyConfig2
        {
            [Bean]
            public MyModel GetMyModel()
            {
                return new MyModel
                {
                    Name = "yuzd"
                };
            }
        }
        
        // autofac打标签模式 
        // 没有指定Key 所以 上面2个都会加载
        builder.RegisterModule(new AutofacAnnotationModule(typeof(MyConfig).Assembly));
        
    

    Bean标签的方法的参数支持注入DI已存在的,或者Value标签

    
            //方法的参数支持注入DI已存在的,或者Value标签
            [Bean]
            public MyModel GetMyModel(OtherClass other,[Value("${connetionString}")] connetionString)
            {
                return new MyModel
                {
                    Name = "yuzd"
                };
            }
            
    
    

    在业务代码里面的使用场景

    1. 使用Key指定可以实现不同的环境配置不同的对象到DI容器
    2. 一些复杂的对象实例可以使用Bean的方式注册的DI容器

    如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,转载文章之后须在文章页面明显位置给出作者和原文连接,谢谢。
     
    分类: c#
    标签: autofac, ioc, DI
  • 相关阅读:
    两个链表的第一个公共节点(Python and C++解法)
    第一个只出现一次的字符(Python and C++解法)
    丑数(Python and C++解法)
    最长不含重复字符的子字符串(Python and C++解法)
    礼物的最大值(Python and C++解法)
    把数字翻译成字符串(Python and C++解法)
    连续子数组的最大和(Python and C++解法)
    最小的k个数(Python and C++解法)
    数组中出现次数超过一半的数字(Python and C++解法)
    字符串的排列(Python and C++解法)
  • 原文地址:https://www.cnblogs.com/xichji/p/11685401.html
Copyright © 2011-2022 走看看