我有以下 XML 配置:
<bean id="bean1" class="Simple"/>
<bean id="bean2" class="Simple"/>
<bean id="tasks" class="java.util.ArrayList">
<constructor-arg>
<list>
<ref bean="bean1" />
<ref bean="bean2" />
</list>
</constructor-arg>
</bean>
<bean id="list" class="Comp">
<property name="tasks" ref="tasks"/>
</bean>
“任务”包含所有 Simple 类型的 bean。这样做的问题是我可能忘记将我配置的 Simple bean 添加到列表中。
我可以以编程方式使用
Map map = context.getBeansOfType(Simple.class);
并使用检索到的 bean 设置列表 bean。
有没有办法只使用 XML 配置来做到这一点?
最佳答案
你的上下文文件应该是这样的:
<bean id="bean1" class="Simple"/>
<bean id="bean2" class="Simple"/>
<bean id="list" class="Comp" autowire="byType"/>
注意添加的 autowire="byType"
和 the autowiring documentation .
关于java - 如何使用 Spring XML 配置来设置包含特定类型所有 bean 列表的 bean 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/943439/