user3427463
user3427463

Reputation: 1

How to create a Spring object type Integer?

Could anyone please help me take a look at this issue since I can only create a Spring object type String? When I try to create another Spring object type, I get the below error

Error

Class Initialization method MessengerLyncSDK2013.Testcases.Test.UnitTest1.ClassInitialize threw exception. System.Configuration.ConfigurationErrorsException: System.Configuration.ConfigurationErrorsException: Error creating context 'spring.root': Error creating object with name 'serverPort' defined in 'config [D:\Working Projects\lync2013\MessengerLyncSDK2013\TestResults\thanh.viet.le_LGVN13307-WIN7 2014-03-17 11_17_21\Out\MessengerLyncSDK2013.DLL.config#spring/objects] line 9' : Could not resolve matching constructor. ---> Spring.Objects.Factory.ObjectCreationException: Error creating object with name 'serverPort' defined in 'config [D:\Working Projects\lync2013\MessengerLyncSDK2013\TestResults\thanh.viet.le_LGVN13307-WIN7 2014-03-17 11_17_21\Out\MessengerLyncSDK2013.DLL.config#spring/objects] line 9' : Could not resolve matching constructor..

Spring object in xml file

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

For more details, I am using visual studio 2010 with C#.

Upvotes: 0

Views: 829

Answers (2)

Leandro S&#225;
Leandro S&#225;

Reputation: 609

Try it, note for factory-method="Parse":

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

See also: How do I create a spring .Net standalone object of type Int32 defined in the IOC context file?

Upvotes: 1

Najera
Najera

Reputation: 2879

You can create an object with all your configuration and then just inject it:

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

Upvotes: 0

Related Questions