4

I know that in Spring you can load all beans of a certain type with:

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

How can I do this in XML? E.g. something like:

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

Or better with a list rather than a map?

jmj
  • 237,923
  • 42
  • 401
  • 438
Puce
  • 37,247
  • 13
  • 80
  • 152
  • Here are links to two questions/answers that should cover the functionality for which you are looking. The first question's answer illustrates how to execute the desired result using @Autowired inside of your class. The second question's answer shows how it can be done using Spring's XML configuration file. - [List Beans By Type](http://stackoverflow.com/questions/1450145/spring-list-beans-by-type) - [Inject all beans of type](http://stackoverflow.com/questions/2799316/how-to-collect-and-inject-all-beans-of-a-given-type-in-spring-xml-configuration) – Kris Babic Apr 19 '11 at 18:49

1 Answers1

1

Use Java Config, which lets you use Java to generate your beans. You can mix it in with existing XML configuration files. See my answer to Spring 3.0.x - context:component-scan result into list

Community
  • 1
  • 1
artbristol
  • 32,010
  • 5
  • 70
  • 103