C# · 12月 20, 2021

AOP 还在配置吗改用打标签模式吧!

<div id=”8750-1547734396925″ style=”font-size: 14px; white-space: pre-wrap; line-height: 1.75;”><span style=”font-size: 28px; font-weight: bold;”>为什么我喜欢打标签来配置AOP
<div style=”font-size: 14px; white-space: pre-wrap; line-height: 1.75;”>1. 配置多很混乱,代码里面很难分辨出来哪些是AOP容器(比如属性注入)
<div style=”font-size: 14px; white-space: pre-wrap; line-height: 1.75;”>2. 对于代码生成器生成的代码里面还需要手动加到配置里面
<div style=”font-size: 14px; white-space: pre-wrap; line-height: 1.75;”>3. 连java spring现在都是清一色的注解来代替xml,这个就是趋势所在
<div style=”font-size: 14px; white-space: pre-wrap; line-height: 1.75;”> 
<div style=”font-size: 14px; white-space: pre-wrap; text-align: left; line-height: 1.25;”>我基于<span style=”font-size: 17px; color: #24292e; background-color: #ffffff;”>Autofac开发了一个基于标签来配置AOP的扩展

builder = <span style=”color: #008000;”>//<span style=”color: #008000;”> 注册autofac打标签模式
builder.RegisterModule(<span style=”color: #0000ff;”>new AutofacAnnotationModule(<span style=”color: #0000ff;”>typeof<span style=”color: #000000;”>(AnotationTest).Assembly));
<span style=”color: #008000;”>//<span style=”color: #008000;”>如果需要开启支持循环注入
<span style=”color: #008000;”>//<span style=”color: #008000;”>builder.RegisterModule(new AutofacAnnotationModule(typeof(AnotationTest).Assembly).SetAllowCircularDependencies(true));
<span style=”color: #0000ff;”>var container =<span style=”color: #000000;”> builder.Build();
<span style=”color: #0000ff;”>var serviceB = container.Resolve();

Name { ; [Bean(AutofacScope = Name { ; }//将class A6以<a href=”https://www.jb51.cc/tag/fulei/” target=”_blank” class=”keywords”>父类</a>B<a href=”https://www.jb51.cc/tag/zhuce/” target=”_blank” class=”keywords”>注册</a>到容器[Bean(</span><span style=”color: #0000ff;”&gt;typeof</span><span style=”color: #000000;”&gt;(B))]</span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; A6:B{}</span></pre> [Bean( School { ; ; } = [Bean(InitMethod = ,DestroyMetnod = Test { ; </span><span style=”color: #0000ff;”&gt;public</span><span style=”color: #000000;”&gt; A29 a29; </span><span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; start(IComponentContext context) { </span><span style=”color: #0000ff;”&gt;this</span>.Test = <span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;bbbb</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;; a29 </span>= context.Resolve<A29><span style=”color: #000000;”&gt;(); } </span><span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; destroy() { </span><span style=”color: #0000ff;”&gt;this</span>.Test = <span style=”color: #0000ff;”&gt;null</span><span style=”color: #000000;”&gt;; a29.Test </span>= <span style=”color: #0000ff;”&gt;null</span><span style=”color: #000000;”&gt;; }}</span></pre> }[Bean(</span><span style=”color: #0000ff;”&gt;typeof</span>(B),<span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;a5</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;)]</span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; A5:B{ </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;string</span> School { <span style=”color: #0000ff;”&gt;get</span>; <span style=”color: #0000ff;”&gt;set</span>; } = <span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;测试a5</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;; </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;override</span> <span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt; GetSchool() { </span><span style=”color: #0000ff;”&gt;return</span> <span style=”color: #0000ff;”&gt;this</span><span style=”color: #000000;”&gt;.School; }}</span></pre> == [Autowired(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;A13</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;)] </span><span style=”color: #0000ff;”&gt;public</span><span style=”color: #000000;”&gt; B b1; [Autowired] </span><span style=”color: #0000ff;”&gt;public</span> B B { <span style=”color: #0000ff;”&gt;get</span>; <span style=”color: #0000ff;”&gt;set</span><span style=”color: #000000;”&gt;; }</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;<a href=”https://www.jb51.cc/tag/required/” target=”_blank” class=”keywords”>required</a><a href=”https://www.jb51.cc/tag/mo/” target=”_blank” class=”keywords”>默</a>认为true 如果装载<a href=”https://www.jb51.cc/tag/cuowu/” target=”_blank” class=”keywords”>错误</a>会抛异常出来。如果指定为false则不抛异常</span>[Autowired(<span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;adadada</span><span style=”color: #800000;”&gt;”</span>,<a href=”https://www.jb51.cc/tag/required/” target=”_blank” class=”keywords”>required</a> = <span style=”color: #0000ff;”&gt;false</span><span style=”color: #000000;”&gt;)] </span><span style=”color: #0000ff;”&gt;public</span><span style=”color: #000000;”&gt; B b1;}</span></pre>”a10″: “aaaaaaaaa1″”list”: [ 1,2,3″dic””name”: “name1″”testInitField”: 1″testInitProperty”: 1 A10([Value()] school,[Value()]List list,[Value()]Dictionary.School =.list =.dic = } </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;string</span> School { <span style=”color: #0000ff;”&gt;get</span>; <span style=”color: #0000ff;”&gt;set</span><span style=”color: #000000;”&gt;; } </span><span style=”color: #0000ff;”&gt;public</span> List<<span style=”color: #0000ff;”&gt;int</span>> list { <span style=”color: #0000ff;”&gt;get</span>; <span style=”color: #0000ff;”&gt;set</span><span style=”color: #000000;”&gt;; } </span><span style=”color: #0000ff;”&gt;public</span> Dictionary<<span style=”color: #0000ff;”&gt;string</span>,<span style=”color: #0000ff;”&gt;string</span>> dic { <span style=”color: #0000ff;”&gt;get</span>; <span style=”color: #0000ff;”&gt;set</span><span style=”color: #000000;”&gt;; } [Value(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;#{testInitField}</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;)] </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;int</span><span style=”color: #000000;”&gt; test;[Value(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;#{testInitProperty}</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;)] </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;int</span> test2 { <span style=”color: #0000ff;”&gt;get</span>; <span style=”color: #0000ff;”&gt;set</span><span style=”color: #000000;”&gt;; }</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;可以直接指定值</span>[Value(<span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;2</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;)]</span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;int</span> test3 { <span style=”color: #0000ff;”&gt;get</span>; <span style=”color: #0000ff;”&gt;set</span><span style=”color: #000000;”&gt;; }}</span></pre> aaaaaaaaa1 1 2 3 name1 A11([Value()] school,> dic { ;