0
votes

I have declared following list using spring util namespace in my spring configuration file:

<util:list id="childList">
        <ref bean="child1"/>
        <ref bean="child2"/>
        <ref bean="child3"/>
</util:list>

where all reference bean are marked with @Componant annotation and their respective beans are creating. But whenever I am trying to Autowired any beans property like:

@Component
public class ListTest{

@Autowired
@Qualifier("childList")
private List<IParent> list;

public List<IParent> getList() {
    return list;
}

}

Gives exception as: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'listTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List com.spring3.componentScanFilterTest.ListTest.list; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.spring3.componentScanFilterTest.IParent] found for dependency [collection of com.spring3.componentScanFilterTest.IParent]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=childList)}

But instead of @Autowired and @Qualifier if I use as: @Resource(name="childList")

It works. Why? As per my understanding @Autowired is used to autowire the property matching by type and @Qualifier is used to select any one bean from multiple ambiguous beans.

Please explain.

2

2 Answers

1
votes

Spring docs says.

As a specific consequence of this semantic difference, beans that are themselves defined as a collection or map type cannot injected through @Autowired, because type matching is not properly applicable to them. Use @Resource for such beans, referring to the specific collection or map bean by unique name.

Hope this clear your doubt.

0
votes

Type matching is not properly applicable to beans which defined as collection.

If you intend to express annotation-driven injection by name, do not primarily use @Autowired - even if is technically capable of referring to a bean name through @Qualifier values. Instead, prefer the JSR-250 @Resource annotation which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.

As a specific consequence of this semantic difference, beans which are themselves defined as a collection or map type cannot be injected via @Autowired since type matching is not properly applicable to them. Use @Resource for such beans, referring to the specific collection/map bean by unique name.

Here: http://docs.spring.io/spring/docs/2.5.x/reference/beans.html#beans-autowired-annotation-qualifiers

You are trying to get list of all beans of type Parent that have the qualifier "childList".