标签:生成器 activate 1.7 20px 获取 def http ffffff name
var builder = new ContainerBuilder(); // 注册autofac打标签模式 builder.RegisterModule(new AutofacAnnotationModule(typeof(AnotationTest).Assembly)); //如果需要开启支持循环注入 //builder.RegisterModule(new AutofacAnnotationModule(typeof(AnotationTest).Assembly).SetAllowCircularDependencies(true)); var container = builder.Build(); var serviceB = container.Resolve<B>();
//把class A 注册到容器 [Bean] public class A { public string Name { get; set; } }
[Bean(AutofacScope = AutofacScope.SingleInstance)] public class A { public string Name { get; set; } }
public class B { } //将class A6以父类B注册到容器 [Bean(typeof(B))] public class A6:B { }
[Bean("a4")]//注册A4到容器 并给他起了一个名字叫a4 假设容器有多个A4被注册就可以用这个名字来区别自动装配 public class A4 { public string School { get; set; } = "测试2"; }
[Bean(InitMethod = "start",DestroyMetnod = "destroy")] public class A30 { [Value("aaaaa")] public string Test { get; set; } public A29 a29; void start(IComponentContext context) { this.Test = "bbbb"; a29 = context.Resolve<A29>(); } void destroy() { this.Test = null; a29.Test = null; } }
public class B { } [Bean(typeof(B),"a5")] public class A5:B { public string School { get; set; } = "测试a5"; public override string GetSchool() { return this.School; } }
[Bean] public class A16 { public A16([Autowired]A21 a21) { Name = name; A21 = a21; } [Autowired("A13")] public B b1; [Autowired] public B B { get; set; } //Required默认为true 如果装载错误会抛异常出来。如果指定为false则不抛异常 [Autowired("adadada",Required = false)] public B b1; }
{ "a10": "aaaaaaaaa1", "list": [ 1, 2, 3 ], "dic": { "name": "name1" }, "testInitField": 1, "testInitProperty": 1, }
[Bean] [PropertySource("/file/appsettings1.json")] public class A10 { public A10([Value("#{a10}")]string school,[Value("#{list}")]List<int> list,[Value("#{dic}")]Dictionary<string,string> dic) { this.School = school; this.list = list; this.dic = dic; } public string School { get; set; } public List<int> list { get; set; } public Dictionary<string,string> dic { get; set; } [Value("#{testInitField}")] public int test; [Value("#{testInitProperty}")] public int test2 { get; set; } //可以直接指定值 [Value("2")] public int test3 { get; set; } }
<?xml version="1.0" encoding="utf-8" ?> <autofac> <a11>aaaaaaaaa1</a11> <list name="0">1</list> <list name="1">2</list> <list name="2">3</list> <dic name="name">name1</dic> </autofac>
[Bean] [PropertySource("/file/appsettings1.xml")] public class A11 { public A11([Value("#{a11}")]string school,[Value("#{list}")]List<int> list,[Value("#{dic}")]Dictionary<string,string> dic) { this.School = school; this.list = list; this.dic = dic; } public string School { get; set; } public List<int> list { get; set; } public Dictionary<string,string> dic { get; set; } }
标签:生成器 activate 1.7 20px 获取 def http ffffff name
原文地址:https://www.cnblogs.com/yudongdong/p/10285392.html