如何创建Spring对象类型Integer?

时间:2014-03-17 04:20:06

标签: c# spring.net

有人可以帮我看看这个问题,因为我只能创建一个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>中定义名称为'serverPort'的对象时出错。 le_LGVN13307-WIN7 2014-03-17 11_17_21 \ Out \ MessengerLyncSDK2013.DLL.config#spring / objects]第9行:无法解析匹配的构造函数。 ---> Spring.Objects.Factory.ObjectCreationException:创建在'config [D:\ Working Projects \ lync2013 \ MessengerLyncSDK2013 \ TestResults \ thanh.viet.le_LGVN13307-WIN7中定义的名称为'serverPort'的对象时出错2014-03-17 11_17_21 \ 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#一起使用。

2 个答案:

答案 0 :(得分: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?

答案 1 :(得分:0)

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

<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>