Inject array of strings to a bean in Spring

java, spring

Solution

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.

Problem

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

Original source

Related problems