Spring.net ContextRegistry.GetContext 2 个接口实现失败

Spring.net ContextRegistry.GetContext fails with 2 interface implementations

我有一个 .net application,它使用 Spring.net。 还有一个 interface 的 2 个实现。应该使用哪一个 - 它基于 configuration file.

Spring 的 bootstrap

<objects>
 <object id="Impl1" name="Impl1" type="namespace.Impl1, IInterface" >
  </object>
  <object id="Impl2" name="Impl2" type="namespace.Impl2, IInterface" >   
  </object>
</objects>

它在

上失败
var appContext = ContextRegistry.GetContext();

错误信息是:

An unhandled exception of type 'System.Configuration.ConfigurationErrorsException' occurred in Spring.Core.dll

Additional information: Unsatisfied dependency expressed through constructor argument with index 2 of type [namespace.IInterface] : No unique object of type [namespace.IInterface] is defined : expected single matching object but found 2: System.Collections.Specialized.OrderedDictionary

我找到了另一种配置,其中自动装配设置为自动检测。

<object id="workspaceExportService" type="namespace.Imp3, Namespace"
          autowire="autodetect" />

此 class Imp3 具有带 IInterface 参数的构造函数。因此,它试图解析 IInterface 的实现,但找到其中两个。

作为解决方案,您可以将 lazy-init="true" 属性添加到对象节点。