Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to inject an int array in spring bean

Tags:

spring

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!

like image 985
Vicky Avatar asked Jan 20 '12 06:01

Vicky


1 Answers

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
like image 84
Moinul Hossain Avatar answered Oct 06 '22 18:10

Moinul Hossain