如何使用spring.net为typealias做属性注入

本文关键字:属性 注入 typealias 何使用 spring net | 更新日期: 2023-09-27 17:52:17

我在使用spring.net创建对象时遇到了一些问题,需要解决这个问题。

<<p> 设置描述/strong>:下面是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文件创建。

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

:在运行应用程序时,我调用ABCHost类的默认构造函数,查看器属性获得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)

你能帮忙找出这个问题的解决办法吗?

如何使用spring.net为typealias做属性注入

TypeAlias是使用非常简单的xml配置定义的。

<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/TypeAlias结构中