7

I have a ServiceListFactoryBean which creates a list of service implementations:

<bean id="services"
      class="org.springframework.beans...ServiceListFactoryBean"
      p:serviceType="ServiceInterface"/>

I can access the services using the applicationContext without a problem:

    final List services = ctx.getBean("services", List.class));

I can also use trad constructor-arg injection successfully:

<bean id="aClass" class="AClass">
    <constructor-arg ref="services"/>
</bean>

But if I try to autowire the dependency

@Autowired @Qualifier("services") private List services;

Then I get a BeanCreationException caused by

FatalBeanException: No element type declared for collection [java.util.List]

I am using Spring 3.0.

Paul McKenzie
  • 19,646
  • 25
  • 76
  • 120

3 Answers3

11

It turns out that the answer is ...

@Resource(name="services") private List services;
Paul McKenzie
  • 19,646
  • 25
  • 76
  • 120
  • 2
    This will indeed work, because now it is getting autowired by name, instead of by type. That was the case previously. May be you knocked the problem off this time. But you must understand why. So, please ponder over the answer provided. Cheers. – Adeel Ansari Jan 28 '10 at 09:40
  • Vinegar -- There are only two beans in the whole project (so far) and regardless of using @Qualifiers or not, or explicitly seting default-autowire to byType or byName, @Autowiring doesn't work for this ServiceListFactoryBean. – Paul McKenzie Jan 28 '10 at 11:30
4

The exception message is from DefaultListableBeanFactory, and it's complining that it can't autowire your field because the List has no generic type (see DefaultListableBeanFactory line 716).

Try adding a generic signature to your field, e.h.

@Autowired @Qualifier("services") private List<Service> services;
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 2
    Nope, this doesn't work I'm afraid. Explicitly set autowire to byType and get: No matching bean of type [ServiceInterface] found for dependency [collection of ServiceInterface] – Paul McKenzie Jan 28 '10 at 11:26
0

what i actually found out today, is that when you need map of bean names to instances of specific interface, there is no need of @Qualifier's and any sort of FactoryBean code. Spring would find and inject candidates for you. @Resource didn't do it's job, just in case.

nefo_x
  • 3,050
  • 4
  • 27
  • 40