
本文共 10102 字,大约阅读时间需要 33 分钟。
������������������java���kotlin������������ ������SpringBoot������������������DI��������������������������������� ������������������������������net������������������������������������������������������
- ���������������
- ������netcore2.0 + framework4.6+
- NUGET��������� Install-Package Autofac.Annotation
������������������autofac������������������������������������������������������
- Component������������������DI������������������������������
- Configuration���������Bean������������������������������������������DI������
- PropertySource���Value���������������������������������������������������DI������
- Autowired������������������������������
������java���spring��������������������������������������������������������������������������������� ������������������������������
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();
AutofacAnnotationModule���������������������
- ���������������Assebly������ ���������������������������������Assebly���������������������������
- ���������������AsseblyName������ (���������������������������AsseblyName������Assebly ���������������)
Component������
���������������������class������(������������������class) ���������������������autofac������ ���������
- ������������������������ ��������� builder.RegisterType
//���class A ���������������[Component]public class A{ public string Name { get; set; }}//������ A������������������������������ ������������������(���������public���������autofac������������������������������)public interface IB{} public class ParentB:IB{ public string Name1 { get; set; }}//���class B ��������������� ��������� B������ParentB��������������� ���������B������IB���������������[Component]public class B:ParentB{ public string Name { get; set; }}
- ������Scope [������������AutofacScope������ ������������������������������AutofacScope.InstancePerDependency]
[Component(AutofacScope = AutofacScope.SingleInstance)] public class A { public string Name { get; set; } }
- ������������������ ��������� builder.RegisterType<A6>().As()
public class B { } [Component(typeof(B))] public class A6:B { }
- ������������������ ��������� builder.RegisterType<A6>().Keyed<A4>("a4")
[Component("a4")] public class A4 { public string School { get; set; } = "������2"; }
- ������������������
- OrderIndex ������������ ���������������������������������������������������������������������������������������������������OrderIndex������������(������autofac������������������)���
- InjectProperties ������������������������ ������������true���
- Autowired ���������������������������Autowired���������������������������
-
- Ownership ������������������ ���������������
- Interceptor ������������������������������������Type
- InterceptorType ��������������� ��������������������� Castle.DynamicProxy��� IInterceptor ��������� ���������������
- Interface ���������������������������������
- Class ���������class������ ���������������������������������������������virtual
- InterceptorKey ������������������������������������������ ������������Key
- InitMethod ������������������������������������������ ������Spring���init-method ������������������(������1������������������IComponentContext)���������������������
- DestroyMetnod ������������Release������������������ ������Spring���destroy-method ���������������������������
[Component(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(); } void destroy() { this.Test = null; a29.Test = null; } }
public class B { } [Component(typeof(B),"a5")] public class A5:B { public string School { get; set; } = "������a5"; public override string GetSchool() { return this.School; } }
Autowired ������������
������������Field Property ���������������Parameter������ ������Field ��� Property ���������������
[Component] 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; }
Value ��� PropertySource
- PropertySource������Spring���������PropertySource ��������������������� ������ xml json������ ������������������
- json���������������
{ "a10": "aaaaaaaaa1", "list": [ 1, 2, 3 ], "dic": { "name": "name1" }, "testInitField": 1, "testInitProperty": 1,}
[Component] [PropertySource("/file/appsettings1.json")] public class A10 { public A10([Value("#{a10}")]string school,[Value("#{list}")]List list,[Value("#{dic}")]Dictionarydic) { this.School = school; this.list = list; this.dic = dic; } public string School { get; set; } public List list { get; set; } public Dictionary dic { get; set; } [Value("#{testInitField}")] public int test; [Value("#{testInitProperty}")] public int test2 { get; set; } //��������������������� [Value("2")] public int test3 { get; set; } }
- xml���������������
aaaaaaaaa1 1
2
3
name1
[Component] [PropertySource("/file/appsettings1.xml")] public class A11 { public A11([Value("#{a11}")]string school,[Value("#{list}")]List list,[Value("#{dic}")]Dictionarydic) { this.School = school; this.list = list; this.dic = dic; } public string School { get; set; } public List list { get; set; } public Dictionary dic { get; set; } }
- ���������PropertySource��������������������������������� appsettings.json���������
AutoConfiguration������ ��� Bean������
[AutoConfiguration] public class TestConfiguration { //Bean������������������AutoConfiguration��������������������������������������� //��������������������� [Bean] private ITestModel4 getTest5() { return new TestModel4 { Name = "getTest5" }; } }
���������build������������������ ������������������������������������������������AutoConfiguration���������class���������������������Bean��������������������������������������������������������������������������������������������� ������������������������������AutoConfiguration���������class���������������������
AutoConfiguration������������������������
- OrderIndex ������������OrderIndex������������������������������������������
- Key ���������������Key������������
������������������������������������������������������������������������������Key = ���test��� ��������� AutoConfiguration������class (new AutofacAnnotationModule(typeof(AnotationTest).Assembly).SetAutofacConfigurationKey("test"));
Bean������������������������
- Key ���������������Key������������ ������������������������������������������ ������������Key���������
AutofacAnnotation���������������autofac���������������������������
public class AutofacAutowiredResolveBenchmark { private IContainer _container; [GlobalSetup] public void Setup() { var builder = new ContainerBuilder(); builder.RegisterType().As ().WithAttributeFiltering(); builder.RegisterType ().As (); builder.RegisterType ().Keyed ("log2"); builder.RegisterType ().WithAttributeFiltering().PropertiesAutowired(); builder.RegisterType ().As ().WithAttributeFiltering().PropertiesAutowired().EnableInterfaceInterceptors() .InterceptedBy(typeof(AsyncInterceptor)); builder.RegisterType ().WithAttributeFiltering().PropertiesAutowired().EnableClassInterceptors() .InterceptedBy(new KeyedService("log2", typeof(AsyncInterceptor))); _container = builder.Build(); } [Benchmark] public void Autofac() { var a1 = _container.Resolve (); var a2= a1.A23.GetSchool(); } }
BenchmarkDotNet=v0.11.3, OS=Windows 10.0.18362Intel Core i7-7700K CPU 4.20GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores.NET Core SDK=2.2.300 [Host] : .NET Core 2.1.13 (CoreCLR 4.6.28008.01, CoreFX 4.6.28008.01), 64bit RyuJIT [AttachedDebugger] DefaultJob : .NET Core 2.1.13 (CoreCLR 4.6.28008.01, CoreFX 4.6.28008.01), 64bit RyuJIT
Method | Mean | Error | StdDev |
---|---|---|---|
Autofac | 30.30 us | 0.2253 us | 0.1997 us |
//��������������� public class AutowiredResolveBenchmark { private IContainer _container; [GlobalSetup] public void Setup() { var builder = new ContainerBuilder(); builder.RegisterModule(new AutofacAnnotationModule(typeof(A13).Assembly)); _container = builder.Build(); } [Benchmark] public void AutofacAnnotation() { var a1 = _container.Resolve(); var a2= a1.A23.GetSchool(); } }
BenchmarkDotNet=v0.11.3, OS=Windows 10.0.18362Intel Core i7-7700K CPU 4.20GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores.NET Core SDK=2.2.300 [Host] : .NET Core 2.1.13 (CoreCLR 4.6.28008.01, CoreFX 4.6.28008.01), 64bit RyuJIT [AttachedDebugger] DefaultJob : .NET Core 2.1.13 (CoreCLR 4.6.28008.01, CoreFX 4.6.28008.01), 64bit RyuJIT
Method | Mean | Error | StdDev |
---|---|---|---|
AutofacAnnotation | 35.36 us | 0.1504 us | 0.1407 us |
发表评论
最新留言
关于作者
