如何使用spring.net为TypeAliases进行属性注入

时间:2016-06-27 06:02:14

标签: c# spring.net

我在使用spring.net创建对象时遇到问题,需要解决方案。

设置说明: 我有以下TypeAliases的配置文件。

<objects xmlns="http://www.springframework.net" >
    <object id="TypeAlias" type="Spring.Objects.Factory.Config.TypeAliasConfigurer, Spring.Core">
        <property name="TypeAliases">
            <dictionary>                
                <entry key="ABCHost" value-ref="XYZ"/> 
            </dictionary>
        </property>
    </object>
     <object id="XYZ" type="ABC.ABCHost, ABCHost" >
        <property name="Viewer" ref="ViewerFactory" />
     </object>
</objects>

我为什么要进行属性注入是因为ObjectPoolFactory使用默认构造函数。在创建对象时,我希望通过下面的spring文件创建此Viewer属性。

<object id="ViewerFactory" type="XYZViewerFactory, XYZViewer" singleton ="true">
  </object>

问题: 在运行应用程序时,我调用了ABCHost类的默认构造函数,viewer属性获取值为XYZViewer。之后我得到一个弹簧错误 -

ServiceProvider.get_AppContext - ServiceProvider.cs(31)
  Spring Error : 
Spring.Objects.Factory.ObjectInitializationException: 
------------------------------------
--- Message
------------------------------------
Invalid value 'ABCHost' for custom type alias - must be a System.String or System.Type.

------------------------------------
--- Stack Trace
------------------------------------
   at Spring.Objects.Factory.Config.AbstractConfigurer.ResolveRequiredType(Object value, String errorContextSource, String errorContext)

你能帮忙找到解决这个问题的方法吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

TypeAlias使用非常简单的xml配置定义OUTSIDE spring / objects:

<configuration>

  <configSections>
    <sectionGroup name="spring">
      <!-- other configuration section handler defined here -->
      <section name="typeAliases" type="Spring.Context.Support.TypeAliasesSectionHandler, Spring.Core"/> 
     </sectionGroup>
  </configSections>

  <spring>
    <typeAliases>
       <alias name="WebServiceExporter" type="Spring.Web.Services.WebServiceExporter, Spring.Web"/>
    </typeAliases>
  </spring>

</configuration>

请查看docs以了解详情。

TypeAlias在spring / typeAliases结构中定义。