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
util:list
is used to create aList
. You are trying to assign that to aString[]
. Do you really need an array?