[英]How to inject with Spring.NET multiple implementations from common interface?
Unity has a great feature providing a way to inject multiple implementations from a common interface. Unity具有一项很棒的功能,它提供了一种从公共接口注入多个实现的方法。
The way it resolves a specific implementation is by name. 解决特定实现的方式是按名称命名。
I would like to know how I could do the same thing with Spring.NET. 我想知道如何使用Spring.NET做同样的事情。
For example I have the following objects where both A and B resolvers use the same interface. 例如,我有以下对象,其中A和B解析器使用相同的接口。
Follows the spring configuration: 遵循弹簧配置:
<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd http://www.springframework.net/aop http://www.springframework.net/xsd/spring-aop.xsd" xmlns:db="http://www.springframework.net/database" xmlns:aop="http://www.springframework.net/aop" xmlns:tx="http://www.springframework.net/tx">
<object id="A" type="Program.Mappers.Resolvers.A, Program"></object>
<object id="B" type="Program.Mappers.Resolvers.B, Program"></object>
</objects>
Spring.NET does not accept this because both objects use the same interface. Spring.NET不接受此操作,因为两个对象使用相同的接口。
How can I solve it? 我该如何解决?
Any IApplicationContext is a IObjectFactory and this interface has methods to perform that. 任何IApplicationContext都是IObjectFactory,并且此接口具有执行该操作的方法。
//
// Summary:
// Return an instance (possibly shared or independent) of the given object name.
//
// Parameters:
// name:
// The name of the object to return.
//
// Type parameters:
// T:
// The type of the object to return.
//
// Returns:
// The instance of the object.
//
// Exceptions:
// Spring.Objects.Factory.NoSuchObjectDefinitionException:
// If there's no such object definition.
//
// Spring.Objects.Factory.ObjectNotOfRequiredTypeException:
// If the object is not of the required type.
//
// Spring.Objects.ObjectsException:
// If the object could not be created.
//
// Remarks:
// This method allows an object factory to be used as a replacement for the
// Singleton or Prototype design pattern.
// Note that callers should retain references to returned objects. There is
// no guarantee that this method will be implemented to be efficient. For example,
// it may be synchronized, or may need to run an RDBMS query.
// Will ask the parent factory if the object cannot be found in this factory
// instance.
T GetObject<T>(string name);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.