如何把SpringBoot的注解注入功能移植到.Net平台(详细步骤)

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:35   1824   0

最近在公司用java和kotlin写接口, 发现SpringBoot的注解来配置DI容器的功能非常的好用: 找了一下发现没有一个net的框架实现了,所以我决定自己写一个!

开源地址:https://github.com/yuzd/Autofac.Annotation
支持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<B>();

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】
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 必须是无参数的方法
[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<A29>();
}

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";9

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP