【问题标题】:Spring: reference all beans of type FooSpring:引用 Foo 类型的所有 bean
【发布时间】:2011-04-19 18:10:39
【问题描述】:

我知道在 Spring 中你可以加载某种类型的所有 bean:

ClassPathResource res = new ClassPathResource("spring_foo.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
Map<String, Foo> beans = factory.getBeansOfType(Foo.class);

如何在 XML 中做到这一点? 例如。类似:

<bean id="fooHandler" class="com.mycompany.FooHandler">
    <property name="foos">
    <map beanType="com.mycompany.Foo" / >
    </property>
</bean>

还是使用列表而不是地图更好?

【问题讨论】:

  • 这里有两个问题/答案的链接,应该涵盖您正在寻找的功能。第一个问题的答案说明了如何在课堂内使用 @Autowired 执行所需的结果。第二个问题的答案显示了如何使用 Spring 的 XML 配置文件来完成。 - List Beans By Type - Inject all beans of type

标签: java spring javabeans


【解决方案1】:

使用Java Config,它允许您使用Java 生成您的bean。您可以将它与现有的 XML 配置文件混合使用。查看我对Spring 3.0.x - context:component-scan result into list的回复

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2019-07-25
    • 2016-06-16
    相关资源
    最近更新 更多