Java 如何使用Spring创建不同类型对象的列表,而不使用bean
我有一个类,它将有一个命令列表,它必须一个接一个地执行。命令可能会重复,我不想为每个命令创建bean 我的想法是:Java 如何使用Spring创建不同类型对象的列表,而不使用bean,java,spring,Java,Spring,我有一个类,它将有一个命令列表,它必须一个接一个地执行。命令可能会重复,我不想为每个命令创建bean 我的想法是: <?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.
<?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>
我做错了什么?如何让spring调用构造函数?您可以创建一个实现
PropertyEditorSupport
的类,以便spring知道如何从字符串转换为自定义域对象
例如:
public class CommandPropertyEditor extends PropertyEditorSupport {
public void setAsText(String text) {
// create a command from the given text
Command command = Command.createFromString(text);
setValue(command);
}
}
然后将其作为弹簧的参考:
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="Command" value="CommandPropertyEditor"/>
</map>
</property>
</bean>
很好。那么,问题在哪里?你建议好的设计。只是实现它。对不起,我忘了设置异常。问题是它不起作用。
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="Command" value="CommandPropertyEditor"/>
</map>
</property>
</bean>