Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject array of strings to a bean in Spring

Tags:

java

spring

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://            www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="test" class="com.Test">
        <constructor-arg>
            <list>
                <value>aa</value>
                <value>bb</value>
                <value>cc</value>
            </list>
        </constructor-arg>
    </bean>
</beans>

This is my current XML. If only Test took a List - everything would be fine.

The problem is that Test takes an array of strings.

How to do it in Spring?

like image 677
Queequeg Avatar asked Nov 23 '12 11:11

Queequeg


1 Answers

You should use:

<constructor-arg>
    <array>
        <value>aa</value>
        <value>bb</value>
        <value>cc</value>
    </array>
</constructor-arg>

An array can contain multiple inner bean, ref, collection, or value elements. This configuration element will always result in an array, even when being defined e.g. as a value for a map with value type Object.

Also you can specify a value type which you will be passing:

<array value-type="java.lang.String">
  <value>aa</value>
  <value>bb</value>
  <value>cc</value>
</array>

value-type

The default Java type for nested values. Must be a fully qualified class name.

like image 83
Paulius Matulionis Avatar answered Oct 04 '22 05:10

Paulius Matulionis