做微信的微网站费用多少c 网站建设设计报告
news/
2025/9/23 2:29:01/
文章来源:
做微信的微网站费用多少,c 网站建设设计报告,工程造价管理,网站做优化Autofac.Annotation框架是我用.netcore写的一个DI框架#xff0c;基于Autofac参考 Spring注解方式所有容器的注册和装配,切面,拦截器等都是依赖标签来完成。开源地址#xff1a;https://github.com/yuzd/Autofac.Annotation本期讲的是最新实现的功能有条件的DI有些时候我们想… Autofac.Annotation框架是我用.netcore写的一个DI框架基于Autofac参考 Spring注解方式所有容器的注册和装配,切面,拦截器等都是依赖标签来完成。开源地址https://github.com/yuzd/Autofac.Annotation本期讲的是最新实现的功能有条件的DI有些时候我们想要满足xxx条件才把一个类注册到容器里面。比如如何切换Services如果是Spring可以根据条件注册Bean和Configuration。所以我参考Spring的条件注解也在我的Autofac.Annotation框架中也实现了以下注解注解使用方式备注Conditional打在class或者方法上面条件加载,自定义实现的ConditionOnBean打在标有Bean注解的方法上面条件加载ConditionOnMissingBean打在标有Bean注解的方法上面条件加载ConditionOnClass打在class或者方法上面条件加载ConditionOnMissingClass打在class或者方法上面条件加载ConditionOnProperty打在class或者方法上面条件加载ConditionOnProperties打在class或者方法上面条件加载DependsOn可以配合Bean和Component使用A的实例化依赖另一个B的实例化,但是A并不需要持有一个B的对象下面来讲讲使用方法ConditionOnBean和ConditionOnMissingBean这2个注解是只能配合Bean注解一起使用且只能打在方法上面不能打在class上面ConditionOnBean的意思是如果指定的类已经被注册的话我才要注册。[AutoConfiguration]
public class Test10Config
{[Bean][ConditionOnBean(typeof(Test10Model3))]public Test10Model5 getTest10Model5(){Console.WriteLine(registered Test10Model5);return new Test10Model5();}}上面的代码的意思是如果Test10Model3被注册的话才会注册Test10Model5ConditionOnMissingBean的意思是如果指定的类没被注册的话我才要注册。[AutoConfiguration]
public class Test10Config
{[Bean][ConditionOnMissingBean(typeof(Test10Model1))]public Test10Model3 getTest10Model3(){Console.WriteLine(registered Test10Model3);return new Test10Model3();}}上面的代码的意思是如果Test10Model1没被注册的话才会注册Test10Model3ConditionOnClass和ConditionOnMissingClass这2个注解是配合Compoment或者AutoConfigurationPointCut等注解一起使用可以打在class和method上面该注解的参数需要填入类的完整名称ConditionOnClass的意思是如果当前运行环境存在指定的类的话就注册[Bean]
[ConditionOnClass(Autofac.Annotation.Test.Test10Model2,Autofac.Configuration.Test)]
public Test10Model6 getTest10Model6()
{//找的到class 所以可以注册Test10Model6Console.WriteLine(registered Test10Model6);return new Test10Model6();
}ConditionOnMissingClass的意思是如果当前运行环境不存在指定的类的话就注册[Bean]
[ConditionOnMissingClass(Autofac.Annotation.Test.test10.Test10Model2,xxxx)]
public Test10Model7 getTest10Model7()
{//找不到class 所以注册Test10Model7Console.WriteLine(registered Test10Model7);return new Test10Model7();
}ConditionOnProperty和ConditionOnProperties这2个注解可以配合Bean,Compoment,AutoConfigurationPointCut等注解一起使用可以打在class和method上面意思是如果数据源(读取当前项目的appsettings.json)指定的key对应的值为xxx时或者不存在指定的key就注册appsettings.json{onproperty: on
}里面存在指定的key为xxx时就注册[Bean]
[ConditionalOnProperty(onproperty, on)]
public Test10Model8 getTest10Model8()
{//因为配置文件onproperty的值为on 所以会注册Console.WriteLine(registered Test10Model8);return new Test10Model8();
}或者不存在指定的key[Bean]
[ConditionalOnProperty(onproperty1, matchIfMissing true)]
public Test10Model10 getTest10Model10()
{//由于配置文件里面没有onproperty1 所以会注册Console.WriteLine(registered Test10Model10);return new Test10Model10();
}当想要指定多个值同时满足的话就用ConditionOnProperties道理是一样的~Conditional这个注解接受一个实现了ICondition接口的Type类型的参数。具体的判断条件由自己实现(比如上面的几个条件注解都满足不了你那你就用这个注解搭配自定义的条件)首先我们定义一个class实现ICondition接口的ShouldSkip方法下面的类的意思看注释应该可以明白public class Test10Condition : ICondition
{/// summary/// 只有当 windows 系统下才被注册/// /summary/// param namecontext/param/// param namemetadata/param/// returns返回true代表不满足条件那就不会被注册到容器/returnspublic bool ShouldSkip(IComponentRegistryBuilder context, object metadata){if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)){//是linux系统 就不注册return true;}if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)){//是mac系统 也不注册return true;}//是windows系统 那就注册return false;}
}下面我们来使用上面的条件用Conditional注解打在方法上面这个条件表明了只有在windows平台才会将Test10Model1注册到容器中[AutoConfiguration]
public class Test10Config
{[Bean][Conditional(typeof(Test10Condition))]public Test10Model1 getTest10Model1(){Console.WriteLine(registered Test10Model1);return new Test10Model1();}}上面的例子是结合Bean注解一起使用Conditional注解还可以打在class上面结合Compoment或者AutoConfiguration注解来实现满足条件才注册Conditional也是上面几个其他注解的父类image不同的是上面几个其他注解的构造方法都指定了自己默认的实现类。这样面向接口设计的好处是在注册的初始化阶段针对验证条件的逻辑就可以统一处理只搜集 Conditional或者Conditional的子类注解且约束了条件判断的类统一得实现ICondition接口。DependsOn该注解可以配合Bean和Component注解一起使用和ConditionXXX系列不一样是用来表示一个 A的实例化依赖另一个B的实例化 但是A并不需要持有一个B的对象[Bean]
[DependsOn(typeof(Test12Bean4))]
public Test12Bean3 get13()
{Debug.WriteLine(new Test12Bean3);return new Test12Bean3 { Hello world };
}[Bean]
public Test12Bean4 get14()
{Debug.WriteLine(new Test12Bean4);result.Add(get14);return new Test12Bean4 { Hello world };
}上面的意思是在需要加载Test12Bean3实例(还没)的时候由于设置了DependsOn类Test12Bean4先去加载Test12Bean4[Component]
[DependsOn(typeof(Test12Bean8))]
public class Test12Bean7
{public Test12Bean7(){//Console.WriteLine(然后我在加载) }public string Hello { get; set; }
}[Component]
public class Test12Bean8
{public Test12Bean8(){//Console.WriteLine(我先加载)}public string Hello { get; set; }
}上面的意思是在需要加载Test12Bean7的实例的时候先去加载Test12Bean8好了有条件的DI介绍到此更多教程请参考项目wiki(教程很详细哦别忘记给个star)https://github.com/yuzd/Autofac.Annotation/wiki我是正东,学的越多不知道也越多。写代码的乐趣在于你可以展现你发现的美。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/911118.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!