【问题标题】:How to do property injection for TypeAliases using spring.net如何使用 spring.net 为 TypeAliases 进行属性注入
【发布时间】:2016-10-29 01:06:15
【问题描述】:

我在使用 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 类的默认构造函数,并且查看器属性获取值为 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)

您能帮忙找到这个问题的解决方案吗?提前致谢。

【问题讨论】:

  • 找到任何解决方案了吗?

标签: c# spring.net


【解决方案1】:

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 结构中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多