I have a class which will have a list of commands that it must execute one after another. Commands may be repeated and I don’t want to create a bean for each Command.
What I have in mind is something like:
<?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="parent" class="Parent">
<property name="commands">
<list value-type="Command">
<value>OneCommand</value>
<value>OtherCommand</value>
<value>OneCommand</value>
</list>
</property>
</bean>
</beans>
And for each value the constructor of the class is called and a new Command object added to the list.
Thing is that when I run a test with that xml file the following exception:
...
Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.fideliapos.middleoffice.provisioning.ProvisioningCommand] for property 'commands[0]': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)
at org.springframework.beans.TypeConverterDelegate.convertToTypedCollection(TypeConverterDelegate.java:520)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:173)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:447)
... 42 more
What am I doing wrong? How can I get spring to call the constructor?
You may create a class implementing
PropertyEditorSupportso that Spring knows how to convert from a String to your custom domain object.Example:
And then reference it for Spring: