Announcement Announcement Module
Collapse
No announcement yet.
Injecting String Array? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Injecting String Array?

    Does spring support injection of Spring Array?

    The following code does not work. It is just an illustration of what I trying to achieve.

    Code:
    in code
    
    String[] types;
    
    public void setTypes(String[] types) {
      this.types = types;
    }
    
    in spring config
    
    <bean id="types" class="java.lang.String[]">
      <list>
          <value>a</value>
          <value>b</value>
      </list>
    </bean>

  • #2
    This config should do it:
    Code:
    <property name="componentVector">
    			<list>
    				<value>a</value>
    				<value>b</value>
    			</list>
    		</property>

    Comment


    • #3
      Originally posted by Andrei Stefan View Post
      This config should do it:
      Code:
      <property name="componentVector">
      			<list>
      				<value>a</value>
      				<value>b</value>
      			</list>
      		</property>
      Your code will not work cos I want to inject type String[] and not a collection type like list, set, map or props.

      Comment


      • #4
        Hi,

        Spring will do the conversation for you, if you separat your values through a comma.

        Like here

        Code:
        	<bean name="test" class="Test">
        		<property name="values" value="hugo,emil"></property>
        	</bean>
        Where values is a String[] array.

        Regards
        lyserg

        Comment


        • #5
          Originally posted by Lyserg View Post
          Hi,

          Spring will do the conversation for you, if you separat your values through a comma.

          Like here

          Code:
          	<bean name="test" class="Test">
          		<property name="values" value="hugo,emil"></property>
          	</bean>
          Where values is a String[] array.

          Regards
          lyserg
          Thanks it works.

          Comment


          • #6
            I suppose you haven't tried the config I proposed. Spring is able to convert the list into an array.
            spring-beans-2.0.dtd:
            <!--
            A list can contain multiple inner bean, ref, collection, or value elements.
            Java lists are untyped, pending generics support in Java1.5,
            although references will be strongly typed.
            A list can also map to an array type.The necessary conversion
            is automatically performed by the BeanFactory.
            -->
            <!ELEMENTlist(
            (bean|ref|idref|value|null|list|set|map|props)*
            )>

            Comment


            • #7
              I assume the following is what you meant?

              in code

              Code:
              private String[] componentVector
              
              public void setComponentVector(String[] componentVector) {
                this.componentVector = componentVector;
              }
              in config

              Code:
              <property name="componentVector">
                <list>
                  <value>a</value>
                  <value>b</value>
                </list>
              </property>
              Yup I did try, but it did not work. And I am using Spring 2.x

              Comment


              • #8
                This works on my machine. When you say it doesn't work, do you get an exception?

                Comment


                • #9
                  My bad, I tried it again and it worked.

                  Thanks Andrei, karldmoore

                  Comment


                  • #10
                    Glad it worked for you! I was staring at that for a while trying to see what might have possibly been wrong but I all looked fine. I guess that's because it was . Glad it's sorted.

                    Comment


                    • #11
                      Does this work for integers and doubles?

                      I got both techniques to work with strings. Does either work with int or Integer? I could not get either of these to work with ints or integers!
                      Thanks,
                      Siegfried

                      Code:
                        <bean id="siegfriedStringArray" class="com.SIGNITEK.Demo.ch4.SiegfriedsArray">
                        <property name="arrayString2" value="xyz,423"/>
                        <property name="arrayString">
                          <list>
                            <value>abc</value>
                            <value>def</value>
                          </list>
                        </property>
                      <!-- 
                        <property name="arrayInteger" value="34, 5523">
                            <list>
                            <value>34</value>
                            <value>5523</value>
                            </list>
                      
                        </property>
                      -->
                        </bean>

                      Comment


                      • #12
                        another suggestion...

                        Sorry for replying this 2-year old post...
                        This is the second item I search for the same problem in google...

                        And... I got a good answer to the question in first post

                        Take an easy problem (example?) I got...

                        Code:
                          <bean id="shared.context" 
                                class="org.springframework.context.support.ClassPathXmlApplicationContext">
                        
                            <constructor-arg>
                              <list>
                                <value>shared-applicationContext.xml</value>
                                <value>shared-jndiDataAccessContext.xml</value>
                              </list>      
                            </constructor-arg>
                        
                          </bean>
                        And that "ClassPathXmlApplicationContext" class only accept "String[]" as constructor argument.

                        For <list>, Spring will do the type conversion from "Collection" to "String[]" if "Collection" is not possible to be a input parameter. (sure in this case..)

                        When I start up the web application,

                        it will simply throw an exception in log4j with DEBUG level from spring.

                        Code:
                        [2009-09-22 23:44:41,871][D][org.springframework.beans.TypeConverterDelegate]  Cannot create copy of Collection type [java.util.ArrayList] - injecting original Collection as-is
                        java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Collection
                        But it still works... as normal. Everything works.
                        The exception only tells you that spring has to do "Type conversion" from Collection to array String[].


                        Long story for short...

                        Code:
                          <bean id="shared.context" 
                                class="org.springframework.context.support.ClassPathXmlApplicationContext">
                        
                            <constructor-arg>
                              <array>
                                <value>shared-applicationContext.xml</value>
                                <value>shared-jndiDataAccessContext.xml</value>
                              </array>      
                            </constructor-arg>
                        
                          </bean>
                        Spring provides a better element "array" for this kind of usage.


                        No more confusing between list & array now

                        Comment


                        • #13
                          Originally posted by siegfried View Post
                          I got both techniques to work with strings. Does either work with int or Integer? I could not get either of these to work with ints or integers!
                          Thanks,
                          Siegfried

                          Code:
                            <bean id="siegfriedStringArray" class="com.SIGNITEK.Demo.ch4.SiegfriedsArray">
                            <property name="arrayString2" value="xyz,423"/>
                            <property name="arrayString">
                              <list>
                                <value>abc</value>
                                <value>def</value>
                              </list>
                            </property>
                          <!-- 
                            <property name="arrayInteger" value="34, 5523">
                                <list>
                                <value>34</value>
                                <value>5523</value>
                                </list>
                          
                            </property>
                          -->
                            </bean>

                          you may try using "array" instead of "list" as in my previous post

                          Comment

                          Working...
                          X