how to inject an int array in spring bean

17,843

Solution 1

There's a way to put like this:

<beans:bean id="myBean" class="MyClass">
  <beans:property name="myIntArray" >
    <beans:list>
      <beans:value>1</beans:value>
      <beans:value>2</beans:value>
      <beans:value>3</beans:value>
   </beans:list>
  </beans:property>
</beans:bean>

But as you require these values to be read from a properties file,Icouldn't find a way to read from properties file :-(

But I have an ugly fix for it. Change your class to something like this:

    Class MyCLass
    {
        private Integer[] myIntArray;
        private String[] myIntArrayStr;

        public Integer[] getMyIntArray(){
            return this.myIntArray;
        }
        public void setMyIntArray(Integer[] intArray){
            this.myIntArray=intArray;
        }
        public void setMyIntArrayStr(String[] myIntArrayStr) {
           this.myIntArrayStr = myIntArrayStr;
           //we are going to read the values as a string array and set out integer array inside this setter
           int i=0;
           Integer[] myInts = new Integer[myIntArrayStr.length];
           for(String s: myIntArrayStr){
               myInts[i]=Integer.parseInt(s);
               i++;
           }
           setMyIntArray(ints);
       }
   }

Write in the xml as follows:

<beans:bean id="myBean" class="MyClass">
      <beans:property name="myIntArrayStr">
       <beans:value>
        ${myvalues} <!-- this is gonna come from properties file as previously was -->
       </beans:value>
      </beans:property>

    </beans:bean>

Hope this helps.

Solution 2

Seperating the values with comma should do it

if your class looks something like this

Class MyCLass
{
    private Integer[] myIntArray;

    public Integer[] getMyIntArray(){
        return this.myIntArray;
    }
    public void setMyIntArray(Integer[] intArray){
        this.myIntArray=intArray;
    }
}

Your context file should have something like this:

<bean id="myBean" class="MyClass">
    <property name="myIntArray" value="1,2,3,4,5"></property>
</bean>

if you want to user a properties file:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:myProps.properties"/>
</bean>
<bean id="myBean" class="MyClass">
    <property name="myIntArray" value="${myvalues}"></property>
</bean>

In you myProps.properties file

myvalues=1,2,3,4,5
Share:
17,843

Related videos on Youtube

Vicky
Author by

Vicky

A software developer with zeal to learn new things all the time!!

Updated on June 04, 2022

Comments

  • Vicky
    Vicky almost 2 years

    I have a list of integers like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

    I want to use it as an integer array in my POJO.

    However, I do not want it inside my class, but want to externalize it into the properties file and then inject it in my xml as a property of the class.

    How to do it ?

    Thanks for reading!

  • Dave Newton
    Dave Newton about 12 years
    Geez, you waited 20 minutes; have some patience.
  • Vicky
    Vicky almost 12 years
    @Rifat: This is not working... I am getting the below error: Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer[]' for property 'myCodes'; nested exception is java.lang.NumberFormatException: For input string: "1000,2000,3000,4000,5000"
  • Clemens Klein-Robbenhaar
    Clemens Klein-Robbenhaar almost 9 years
    If you add a <bean id="conversionService" class="org.springframework.context.support.ConversionService‌​FactoryBean"> this should work since Spring 3.0