为什么我喜欢打标签来配置AOP
1. 配置多很混乱,代码里面很难分辨出来哪些是AOP容器(比如属性注入)
2. 对于代码生成器生成的代码里面还需要手动加到配置里面
3. 连java spring现在都是清一色的注解来代替xml,这个就是趋势所在
 
我基于Autofac开发了一个基于标签来配置AOP的扩展
 
NUGET :Install-Package Autofac.Annotation
 
开源地址:
帮忙点个star 谢谢!
 
特色
1.打个Bean标签就能注入到AOP
2.打个Autowired标签自动装配注入
3.打个Value标签自动注入配置值(Soure标签配合使用)具体使用方法看下面的例子
4.支持拦截器
5.更多等你发现
 
如何使用
 
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>();
 
AutofacAnnotationModule有两种构造方法
  1. 可以传一个Assebly列表 (这种方式会注册传入的Assebly里面打了标签的类)
  2. 可以传一个AsseblyName列表 (这种方式是先会根据AsseblyName查找Assebly 然后在注册)
 
支持的标签说明
 
Bean标签
说明:只能打在class上面 把某个类注册到autofac容器 例如:
1.无构造方法的方式 等同于 builder.RegisterType();
//把class A 注册到容器
[Bean]
public class A
{
public string Name { get; set; }
}
 
2.指定Scope [需要指定AutofacScope属性 如果不指定为则默认为AutofacScope.InstancePerDependency]
 [Bean(AutofacScope = AutofacScope.SingleInstance)]
public class A
{
public string Name { get; set; }
}
 
3.指定类型注册 等同于 builder.RegisterType().As()
    public class B
{ }
//将class A6以父类B注册到容器
[Bean(typeof(B))]
public class A6:B
{ }
 
4.指定名字注册 等同于 builder.RegisterType().Keyed("a4")
    [Bean("a4")]//注册A4到容器 并给他起了一个名字叫a4 假设容器有多个A4被注册就可以用这个名字来区别自动装配
public class A4
{
public string School { get; set; } = "测试2";
}
 
 
5.其他属性说明
  • InjectProperties 是否默认装配属性 【默认为true】
  • InjectPropertyType 属性自动装配的类型
    1. Autowired 【默认值】代表打了Autowired标签的才会自动装配
    2. ALL 代表会装配所有 等同于 builder.RegisterType().PropertiesAutowired()
  • AutoActivate 【默认为false】 如果为true代表autofac build完成后会自动创建 具体请参考 autofac官方文档
  • Ownership 【默认为空】 具体请参考 autofac官方文档
  • Interceptor 【默认为空】指定拦截器的Type
  • InterceptorType 拦截器类型 拦截器必须实现 Castle.DynamicProxy的 IInterceptor 接口, 有以下两种
    1. Interface 【默认值】代表是接口型
    2. Class 代表是class类型 这种的话是需要将要拦截的方法标virtual
  • InterceptorKey 如果同一个类型的拦截器有多个 可以指定Key
  • InitMethod 当实例被创建后执行的方法名称 类似Spring的init-method 可以是有参数(只能1个参数类型是IComponentContext)和无参数的方法
  • DestroyMetnod 当实例被Release时执行的方法 类似Spring的destroy-method 必须是无参数的方法
 
  [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;
}
}
 
Autowired 自动装配
可以打在Field Property 构造方法的Parameter上面 其中Field 和 Property 支持在父类
 
    [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;
}
 
Value 和 PropertySource
 
PropertySource类似Spring里面的PropertySource 可以指定数据源 支持 xml json格式 支持内嵌资源
1.json格式的文件
{
"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("")]
public int test3 { get; set; }
}
2. xml格式的文件
<?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; }
}
3.不指定PropertySource的话会默认从工程目录的 appsettings.json获取值

AOP 还在配置吗改用打标签模式吧!的更多相关文章

  1. [10] AOP的注解配置

    1.关于配置文件 首先在因为要使用到扫描功能,所以xml的头文件中除了引入bean和aop之外,还要引入context才行: <?xml version="1.0" enco ...

  2. Spring AOP-xml配置

    在spring AOP(一)中介绍了AOP的基本概念和几个术语,现在学习一下在XML中如何配置AOP. 在XML中AOP的配置元素有以下几种: AOP配置元素 描述 <aop:config> ...

  3. TZ_05_Spring_基于AOP的xml配置

    1.分析    1>首先我们有一个Service需要增强 将Service增加一个日志(Logger)          2>写了一个日志的通知并且它可以对Service进行日志增强   ...

  4. Spring AOP及事务配置三种模式详解

    Spring AOP简述 Spring AOP的设计思想,就是通过动态代理,在运行期对需要使用的业务逻辑方法进行增强. 使用场景如:日志打印.权限.事务控制等. 默认情况下,Spring会根据被代理的 ...

  5. AOP切入点的配置

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  6. 【Autofac打标签模式】AutoConfiguration和Bean

    [ Autofac打标签模式]开源DI框架扩展地址: https://github.com/yuzd/Autofac.Annotation/wiki *:first-child { margin-to ...

  7. 【Autofac打标签模式】PropertySource和Value

    [ Autofac打标签模式]开源DI框架扩展地址: https://github.com/yuzd/Autofac.Annotation/wiki *:first-child { margin-to ...

  8. nginx 配置支持URL HTML5 History 模式 与 设置代理

    拾人牙慧:https://segmentfault.com/q/1010000007140360 nginx 配置支持URL HTML5 History 模式 location / { try_fil ...

  9. 【Autofac打标签模式】Component和Autowired

    [ Autofac打标签模式]开源DI框架扩展地址: https://github.com/yuzd/Autofac.Annotation/wiki Componet标签把类型注册到DI容器 1. 把 ...

随机推荐

  1. 一篇读懂HTTPS:加密原理、安全逻辑、数字证书等

    1.引言 HTTPS(全称: Hypertext Transfer Protocol Secure,超文本传输安全协议),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.本文,就来深入介绍下其 ...

  2. nginx反向代理配置

    最近在项目中使用nginx反向代理,根据不同的请求路径,将请求分发到不同服务.下面的示例主要完成如下功能 /prod/路径的请求分发到prod服务 /test/路径的请求分发到test服务 创建文件夹 ...

  3. .NET之微信消息模板推送

    最近在项目中使用到了微信消息模板推送的功能,也就是将对应的消息推送到对应的用户微信上去,前提是你必须要有一个微信公众号并且是付费了的才会有这个功能,还有就是要推送的用户必须是的关注了你的微信公众号的. ...

  4. [Swift]LeetCode1036.逃离大迷宫 | Escape a Large Maze

    In a 1 million by 1 million grid, the coordinates of each grid square are (x, y) with 0 <= x, y & ...

  5. Python爬虫入门教程 46-100 Charles抓取手机收音机-手机APP爬虫部分

    1. 手机收音机-爬前叨叨 今天选了一下,咱盘哪个APP呢,原计划是弄荔枝APP,结果发现竟然没有抓到数据,很遗憾,只能找个没那么圆润的了.搜了一下,找到一个手机收音机 下载量也是不错的. 2. 爬虫 ...

  6. JVM回收算法

    根搜索算法 原理:设立若干种根对象,当任何一个根对象到某一个对象均不可达时,则认为这个对象是可以被回收的.一般是对象持有的引用指向该对象不可达 在JAVA语言中,可以当做GC roots的对象有以下几 ...

  7. pycharm安装svn插件

    弄了svn的服务端和客户端,为了方便我pycharm的使用,我又在pycharm里进行了配置,要用到subversion 下载 https://sourceforge.net/projects/win ...

  8. 分布式事务解决方案FESCAR

    项目地址:FESCAR 以下是官网的文档.简介2019年,Fescar 是 阿里巴巴 开源的 分布式事务中间件,以 高效 并且对业务 0 侵入 的方式,解决 微服务 场景下面临的分布式事务问题. 1. ...

  9. 版本控制工具——Git常用操作(下)

    本文由云+社区发表 作者:工程师小熊 摘要:上一集我们一起入门学习了git的基本概念和git常用的操作,包括提交和同步代码.使用分支.出现代码冲突的解决办法.紧急保存现场和恢复现场的操作.学会以后已经 ...

  10. 2016年第七届蓝桥杯javaB组 试题 答案 解析

    1.煤球数目 有一堆煤球,堆成三角棱锥形.具体: 第一层放1个, 第二层3个(排列成三角形), 第三层6个(排列成三角形), 第四层10个(排列成三角形), .... 如果一共有100层,共有多少个煤 ...