Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a bean of String array in Spring (using XML configuration)

Tags:

java

spring

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

like image 485
Niranjan Avatar asked Dec 11 '25 17:12

Niranjan


2 Answers

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.

like image 174
Sotirios Delimanolis Avatar answered Dec 13 '25 08:12

Sotirios Delimanolis


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>
like image 23
Ahsan Shah Avatar answered Dec 13 '25 07:12

Ahsan Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!