【问题标题】:How to create a Spring object type Integer?如何创建一个 Spring 对象类型 Integer?
【发布时间】:2014-04-22 05:13:33
【问题描述】:

谁能帮我看看这个问题,因为我只能创建一个 Spring 对象类型 String?当我尝试创建另一个 Spring 对象类型时,出现以下错误

错误

类初始化方法 MessengerLyncSDK2013.Testcases.Test.UnitTest1.ClassInitialize 抛出异常。 System.Configuration.ConfigurationErrorsException:System.Configuration.ConfigurationErrorsException:创建上下文“spring.root”时出错:在“config [D:\Working Projects\lync2013\MessengerLyncSDK2013\TestResults\thanh.viet. le_LGVN13307-WIN7 2014-03-17 11_17_21\Out\MessengerLyncSDK2013.DLL.config#spring/objects] 第 9 行':无法解析匹配的构造函数。 ---> Spring.Objects.Factory.ObjectCreationException:在“配置 [D:\Working Projects\lync2013\MessengerLyncSDK2013\TestResults\thanh.viet.le_LGVN13307-WIN7 2014-03-17 11_17_21 中定义的名称为“serverPort”的对象创建时出错\Out\MessengerLyncSDK2013.DLL.config#spring/objects] 第 9 行':无法解析匹配的构造函数..

xml 文件中的 Spring 对象

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>

  <spring>
    <context>
      <resource uri="config://spring/objects" />
    </context>
    <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">
      <object id="connectServer" type="string">
        <constructor-arg value="server.com"/>
      </object>

      <object id="serverPort" type="System.Int32" factory-method="Copy">
        <constructor-arg index="0">
          <value>5222</value>
        </constructor-arg>
      </object>


    </objects>
  </spring>
</configuration>

有关更多详细信息,我正在使用 visual studio 2010C#

【问题讨论】:

  • 你能写出你用过的代码吗?!
  • IApplicationContext 上下文 = ContextRegistry.GetContext(); int PORT = (int)context.GetObject("serverPort"); GetContext() 失败。

标签: c# spring.net


【解决方案1】:

试试吧,注意 factory-method="Parse"

<object id="MyInt" type="System.Int32" factory-method="Parse">
  <constructor-arg index="0">
    <value>123</value>
  </constructor-arg>
</object>

另请参阅:How do I create a spring .Net standalone object of type Int32 defined in the IOC context file?

【讨论】:

    【解决方案2】:

    您可以使用所有配置创建一个对象,然后注入它:

    <object id="ServerConfig" type"...">        
        <property name="ServerPort" value="5222"/>
        ... 
    </object>
    
    <object id="Server" type"...">
        <!-- Constructor injection -->
        <constructor-arg name="configuration" ref="ServerConfig"/>
        <!-- OR Property injection -->
        <property name="Configuration" ref="ServerConfig"/>
    </object>
    

    【讨论】:

    • 感谢您的建议,但我仍然没有运气。下面是错误信息。类初始化方法 MessengerLyncSDK2013.Testcases.Test.UnitTest1.ClassInitialize 抛出异常。 System.Configuration.ConfigurationErrorsException:System.Configuration.ConfigurationErrorsException:创建上下文'spring.root'时出错:找不到匹配的工厂方法'Copy on Type [System.Int32]。
    • 为什么不创建配置类并注入值?
    • 当它具有 Int32 对象类型时,我无法加载此配置。你有什么想法吗?
    • 或者只是创建配置对象并通过上下文获取它:(ServerConfig)context.GetObject("ServerConfig");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 2012-12-05
    • 2016-07-03
    • 2011-04-10
    • 2011-09-29
    相关资源
    最近更新 更多