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.
The beans can be wired via constructor or properties or setter method. For example, there are two POJO classes Customer and Person. The Customer class has a dependency on the Person. @Autowired annotation is optional for constructor based injection.
In Spring, you can use @Autowired annotation to auto-wire bean on the setter method, constructor , or a field . Moreover, it can autowire the property in a particular bean. We must first enable the annotation using below configuration in the configuration file. We have enabled annotation injection.
By default, the @Autowired annotation implies that the dependency is required. This means an exception will be thrown when a dependency is not resolved. You can override that default behavior using the (required=false) option with @Autowired .
Spring @Autowired annotation is used for automatic dependency injection. Spring framework is built on dependency injection and we inject the class dependencies through spring bean configuration file.
It turns out that the answer is ...
@Resource(name="services") private List services;
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With