2014-10-08 69 views
0

Unity有一个很棒的功能,它提供了从一个通用接口注入多个实现的方法。如何使用Spring.NET从公共接口注入多个实现?

它通过名称解析具体实现的方式。

这是example

我想知道如何用Spring.NET做同样的事情。

例如,我有以下对象,其中A和B解析器都使用相同的接口。

跟随春天的配置:因为这两个对象使用相同的接口

<?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不接受这一点。

我该如何解决?

+0

我添加了完整的XML弹簧配置内容。 – 2014-10-08 14:55:34

回答

1

任何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); 
+0

Spring.Net旨在提高IoC和DI功能,如果您的生态系统的许多类正在从容器请求依赖关系,那么您做错了。 – 2014-10-18 08:50:41

相关问题