[转] Autofac创建实例的方法总结

1、InstancePerDependency

对每一个依赖或每一次调用创建一个新的唯一的实例。这也是默认的创建实例的方式。

官方文档解释:Configure the component so that every dependent component or call to Resolve() gets a new, unique instance (default.)

2、InstancePerLifetimeScope

在一个生命周期域中,每一个依赖或调用创建一个单一的共享的实例,且每一个不同的生命周期域,实例是唯一的,不共享的。

官方文档解释:Configure the component so that every dependent component or call to Resolve() within a single ILifetimeScope gets the same, shared instance. Dependent components in different lifetime scopes will get different instances.

3、InstancePerMatchingLifetimeScope

在一个做标识的生命周期域中,每一个依赖或调用创建一个单一的共享的实例。打了标识了的生命周期域中的子标识域中可以共享父级域中的实例。若在整个继承层次中没有找到打标识的生命周期域,则会抛出异常:DependencyResolutionException

官方文档解释:Configure the component so that every dependent component or call to Resolve() within a ILifetimeScope tagged with any of the provided tags value gets the same, shared instance. Dependent components in lifetime scopes that are children of the tagged scope will share the parent‘s instance. If no appropriately tagged scope can be found in the hierarchy an DependencyResolutionException is thrown.

4、InstancePerOwned

在一个生命周期域中所拥有的实例创建的生命周期中,每一个依赖组件或调用Resolve()方法创建一个单一的共享的实例,并且子生命周期域共享父生命周期域中的实例。若在继承层级中没有发现合适的拥有子实例的生命周期域,则抛出异常:DependencyResolutionException

官方文档解释:Configure the component so that every dependent component or call to Resolve() within a ILifetimeScope created by an owned instance gets the same, shared instance. Dependent components in lifetime scopes that are children of the owned instance scope will share the parent‘s instance. If no appropriate owned instance scope can be found in the hierarchy an DependencyResolutionException is thrown.

5、SingleInstance

每一次依赖组件或调用Resolve()方法都会得到一个相同的共享的实例。其实就是单例模式。

官方文档解释:Configure the component so that every dependent component or call to Resolve() gets the same, shared instance.

6、InstancePerHttpRequest

在一次Http请求上下文中,共享一个组件实例。仅适用于asp.net mvc开发。
官方文档解释:Share one instance of the component within the context of a single HTTP request.

原文出处:http://blog.csdn.net/dhx20022889/article/details/9061483

时间: 2024-08-11 17:50:12

[转] Autofac创建实例的方法总结的相关文章

Autofac创建实例的方法总结 【转】

Autofac创建实例的方法总结 1.InstancePerDependency 对每一个依赖或每一次调用创建一个新的唯一的实例.这也是默认的创建实例的方式. 官方文档解释:Configure the component so that every dependent component or call to Resolve() gets a new, unique instance (default.) 2.InstancePerLifetimeScope 在一个生命周期域中,每一个依赖或调用

Autofac创建实例的方法总结

1.InstancePerDependency 对每一个依赖或每一次调用创建一个新的唯一的实例.这也是默认的创建实例的方式. 官方文档解释:Configure the component so that every dependent component or call to Resolve() gets a new, unique instance (default.) 2.InstancePerLifetimeScope 在一个生命周期域中,每一个依赖或调用创建一个单一的共享的实例,且每一个

Autofac创建实例的方法总结[转]

1.InstancePerDependency 对每一个依赖或每一次调用创建一个新的唯一的实例.这也是默认的创建实例的方式. 官方文档解释:Configure the component so that every dependent component or call to Resolve() gets a new, unique instance (default.) 2.InstancePerLifetimeScope 在一个生命周期域中,每一个依赖或调用创建一个单一的共享的实例,且每一个

关于Java中的几种特殊类与接口,及特殊的创建实例的方法

Java中有一些特殊的类,在教材中讲解的不深,但是确实非常有用的,这里总结一下,里面用到的有网上搜到的内容,这里表示下感谢. 一.成员内部类 成员内部类是在一个内中定义的另外一个类,这个类属于其上的类的成员,叫做成员内部类.可以把该类看做是一个成员,可实例化的成员.该类主要作用是代替结构体功能,当一个类使用到的某个成员是一个结构类型,但是该结构又没有通用性,此时就可以声明为内部类,这样便解决了结构体的问题. 注意:内部类是一个编译时的概念,一旦编译成功,就会成为完全不同的两类.对于一个名为out

利用反射动态创建实例并调用方法

在.Net 中,程序集(Assembly)中保存了元数据(MetaData)信息,因此就可以通过分析元数据来获取程序集中的内容,比如类,方法,属性等,这大大方便了在运行时去动态创建实例. 主要用途: 动态加载DLL,实现插件机制. 实例化DLL中的类型. 执行后期绑定,访问在运行时创建的类型的方法. 首先,新建一个空白解决方案:DllSolution 向解决方案中添加一个类库:RefDll,这将是需要被动态创建的程序集 重命名默认新建的Class1类:Student,这个类将是需要动态创建的类.

Java创建一个类的实例的方法

创建一个类的实例的方法有以下四种 一.通过new 来实现,也是最常用的 二.通过反射机制来实现 Class c = Class.forName("路径"): (1)java.lang.newInstance User use = (User)c.java.newInstance(); (2)java.lang.reflect.Constructor类的newInstance方法 Constructor con = c.getConstructor(String.class); User

Spring使用教程(二)配置bean:静态工厂方法和实例工厂方法

public class Car { private String brand; private double price; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public double getPrice() { return price; } public void setPrice(double price) { this.

Spring 使用实例工厂方法实例化Bean

知识点介绍: 实例工厂的意思是获取对象实例的方法不是静态的,所以你需要首先new工厂类,再调用普通的实例方法. [转载使用,请注明出处:http://blog.csdn.net/mahoking] 操作步骤: 1.创建Speaker对象. public class Speaker { //使用实例工厂方法实例化Bean private String speakerName; public Speaker(String speakerName) { this.speakerName = speak

【OC语法快览】三、创建实例对象

Creating Objects  创建对象 There are two main ways to create an object. The first is the one you saw before: 创建对象主要有两种方法.第一种如下: NSString* myString = [NSString string]; This is the more convenient automatic style. In this case, you are creating an autorel