AOP 还在配置吗改用打标签模式吧!
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>();
- 可以传一个Assebly列表 (这种方式会注册传入的Assebly里面打了标签的类)
- 可以传一个AsseblyName列表 (这种方式是先会根据AsseblyName查找Assebly 然后在注册)
//把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";
}
- InjectProperties 是否默认装配属性 【默认为true】
- InjectPropertyType 属性自动装配的类型
- Autowired 【默认值】代表打了Autowired标签的才会自动装配
- ALL 代表会装配所有 等同于 builder.RegisterType().PropertiesAutowired()
- AutoActivate 【默认为false】 如果为true代表autofac build完成后会自动创建 具体请参考 autofac官方文档
- Ownership 【默认为空】 具体请参考 autofac官方文档
- Interceptor 【默认为空】指定拦截器的Type
- InterceptorType 拦截器类型 拦截器必须实现 Castle.DynamicProxy的 IInterceptor 接口, 有以下两种
- Interface 【默认值】代表是接口型
- 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;
}
}
[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("")]
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; }
}
AOP 还在配置吗改用打标签模式吧!的更多相关文章
- [10] AOP的注解配置
1.关于配置文件 首先在因为要使用到扫描功能,所以xml的头文件中除了引入bean和aop之外,还要引入context才行: <?xml version="1.0" enco ...
- Spring AOP-xml配置
在spring AOP(一)中介绍了AOP的基本概念和几个术语,现在学习一下在XML中如何配置AOP. 在XML中AOP的配置元素有以下几种: AOP配置元素 描述 <aop:config> ...
- TZ_05_Spring_基于AOP的xml配置
1.分析 1>首先我们有一个Service需要增强 将Service增加一个日志(Logger) 2>写了一个日志的通知并且它可以对Service进行日志增强 ...
- Spring AOP及事务配置三种模式详解
Spring AOP简述 Spring AOP的设计思想,就是通过动态代理,在运行期对需要使用的业务逻辑方法进行增强. 使用场景如:日志打印.权限.事务控制等. 默认情况下,Spring会根据被代理的 ...
- AOP切入点的配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- 【Autofac打标签模式】AutoConfiguration和Bean
[ Autofac打标签模式]开源DI框架扩展地址: https://github.com/yuzd/Autofac.Annotation/wiki *:first-child { margin-to ...
- 【Autofac打标签模式】PropertySource和Value
[ Autofac打标签模式]开源DI框架扩展地址: https://github.com/yuzd/Autofac.Annotation/wiki *:first-child { margin-to ...
- nginx 配置支持URL HTML5 History 模式 与 设置代理
拾人牙慧:https://segmentfault.com/q/1010000007140360 nginx 配置支持URL HTML5 History 模式 location / { try_fil ...
- 【Autofac打标签模式】Component和Autowired
[ Autofac打标签模式]开源DI框架扩展地址: https://github.com/yuzd/Autofac.Annotation/wiki Componet标签把类型注册到DI容器 1. 把 ...
随机推荐
- Swashbuckle.AspNetCore3.0的二次封装与使用
关于 Swashbuckle.AspNetCore3.0 一个使用 ASP.NET Core 构建的 API 的 Swagger 工具.直接从您的路由,控制器和模型生成漂亮的 API 文档,包括用于探 ...
- Dubbo 支持哪些序列化协议?
面试题 dubbo 支持哪些通信协议?支持哪些序列化协议?说一下 Hessian 的数据结构?PB 知道吗?为什么 PB 的效率是最高的? 面试官心理分析 上一个问题,说说 dubbo 的基本工作原理 ...
- Fork/Jion框架详解
◆Fork/Jion框架可以干什么◆ 如果你要处理1万条数据,但是你的能力暂时还不够,一个简单快捷的办法就是你可以把每次只处理100条,等到处理100次之后再把所有的结果聚合起来你就处理完了这1万条数 ...
- Eclipse中使用Maven搭建SSM框架
Eclipse中不使用Maven搭建SSM框架:https://www.cnblogs.com/xuyiqing/p/9569459.html IDEA中使用Maven搭建SSM框架:https:// ...
- ASP.NET Core WebApi中使用FluentValidation验证数据模型
原文链接:Common features in ASP.NET Core 2.1 WebApi: Validation 作者:Anthony Giretti 译者:Lamond Lu 介绍 验证用户输 ...
- Ubuntu16.04 部署配置GO语言开发环境 & 注意事项
1. 安装GO 安装go语言包: $ curl -O https://storage.googleapis.com/golang/go1.10.1.linux-amd64.tar.gz 下载完成后 ...
- 移动APP及游戏推广,有预算为什么还起不了量
本文转自公众号:caoz的梦呓,作者是互联网行业内资深人士,这里分享一篇他本人写的经验干货,希望能给大家带来点帮助. 在广告分析师群,起量是他们讨论最多的话题之一. 我们刚做游戏出海业务的时候,第一款 ...
- 为 Eureka 服务注册中心实现安全控制
上一篇Eureka 实现微服务注册发现讲了用 Eureka 实现单体版的服务注册与发现.因为本篇是在上一篇的基础上的一点扩充,所以读此篇之前要保证看了上一篇. Eureka 如果不加安全控制,会存在下 ...
- 微信ChatEmoji表情适配,对微信公众号开发有帮助
最近做微信公众号时发现微信ChatEmoji表情与接受的消息显示表情的问题, 微信表情后面的ChatEmoji显示不出,花了一些时间整理,把pc和手机的表情全部都整理了, 由于有两百多个显示可能有点长 ...
- 0423上课练习(list、while、def)
""" 循环录入3个正整数,求最大值,最小值,总和,平均值 访问列表中的元素: 列表的长度: len(列表名) 索引值的范围:[0,len(列表名)-1] 列表名[索引值 ...