16

I am trying to define a Spring bean of type String[] and now able to find a way to do so. Sample program is shown below:

@Component("sampleClass")
public class SampleClass {
    @Value("#{someArrayId}")
    private String[] someArray;

    public void doWithArray() {
        System.out.println(Arrays.toString(someArray));
    }
}

Spring XML Configuration

<context:annotation-config />
<context:component-scan base-package="com.demo.spring" />

<util:list id="someArrayId">
    <array>
        <value>Tiger</value>
        <value>Lion</value>
    </array>
</util:list>

When I am running the program, I get following exception:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sampleClass': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String[] com.demo.spring.SampleClass.someArray; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.util.ArrayList' to required type 'java.lang.String[]'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.Object[]] to required type [java.lang.String]: no matching editors or conversion strategy found

I kind of understand what Spring is complaining, but I don't know how to fix it.

Appreciate if anyone can help.

Thanks, NN

2
  • 1
    Well obviously util:list is used to create a List. You are trying to assign that to a String[]. Do you really need an array? Commented Mar 9, 2014 at 6:06
  • Yes. The actual use case which I cannot disclose here, requires an array.
    – Niranjan
    Commented Mar 9, 2014 at 6:21

3 Answers 3

23

I don't know if this is done for a reason, but this configuration

<util:list id="someArrayId">
    <array>
        <value>Tiger</value>
        <value>Lion</value>
    </array>
</util:list>

is creating a List bean that contains one element, a Object[] with two String values in it.

If you actually wanted a List with two String values in it, you should have

<util:list id="someArrayId">
    <value>Tiger</value>
    <value>Lion</value>
</util:list>

in which case you could modify your field to be annotated with

@Value("#{someArrayId.toArray(new java.lang.String[0])}")

Spring's EL resolver will be able to parse it and execute the corresponding method, which will convert the List to a String[].

Alternatively, remove the @Value annotation and add a @Resource annotated method

@Resource(name = "someArrayId")
private void init(List<String> bean) {
    this.someArray = bean.toArray(new String[0]);
}

I find this cleaner as it's more descriptive.

1
  • Hi, first of all, thanks for the help; @Value("#{someArrayId.toArray(new java.lang.String[0])}") is the correct answer. Now, I understand your point of @Resource, I will make similar changes. This thread can be closed now.
    – Niranjan
    Commented Mar 9, 2014 at 6:43
15

instead of List, just define array. You can also inject it as configuration to make it less ambiguous. Here another point to note is value-type of array.

<bean id="sampleClass" class="somePackage.SampleClass">
    <property name="someArray">
        <array value-type="java.lang.String">
            <value>Tiger</value>
            <value>Lion</value>
        </array>
    </property>
</bean>
6

try like this

@Component("sampleClass")
public class SampleClass {
    @Value("#{someArrayId.toArray(new String[0])}")
    private String[] someArray;
    ...

<util:list id="someArrayId">
    <value>Tiger</value>
    <value>Lion</value>
</util:list>
1
  • Thanks a lot for answering this. This helped saving my day. I can now close this thread.
    – Niranjan
    Commented Mar 9, 2014 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.