3

In this small app, I have used @Autowired annotation along with @Qualifier annotation to config the dependencies, but still an exception is thrown as mentioned below.

Pizaa class

public class Pizza {

    private Address deliverydest;

    @Autowired
    @Qualifier("ForPizza")
    public void setDeliverydest(Address deliverydest) {
        this.deliverydest = deliverydest;
    }
}

Spring Context Configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <bean id="pizza" class="com.test.Shopping.Pizza" />

    <bean id="Cust1Address" class="com.test.Shopping.Address" />

    <bean id="dest1" class="com.test.Shopping.Address" >
        <qualifier value="ForPizza" />
        <property name="buildingno" value="flat1/door2" />
    </bean>

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

</beans>

The exception thrown is

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pizza': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.test.Shopping.Pizza.setDeliverydest(com.test.Shopping.Address); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.test.Shopping.Address] is defined: expected single matching bean but found 2: Cust1Address,dest1

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.test.Shopping.Pizza.setDeliverydest(com.test.Shopping.Address); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.test.Shopping.Address] is defined: expected single matching bean but found 2: Cust1Address,dest1

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.test.Shopping.Address] is defined: expected single matching bean but found 2: Cust1Address,dest1

.

Now why is the Spring not considering the @Qualifier annotation to find the correct bean dest1 having qualifier value="ForPizza" ?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
aj_blk
  • 304
  • 2
  • 6
  • 16
  • 1
    Because the `AutowiredAnnotationBeanPostProcessor` doesn't know about the `@Qualifier` annotation. Use `` ro if you really want to explicitly configure the parsers add a `CustomAutowireConfigurer` to enable detection of `@Qualifier`. – M. Deinum Jun 26 '14 at 12:26
  • @Deinum, its strange bcoz i was following a tutorial in this [link](http://youtu.be/IVIhVJJGo68), and it worked there. – aj_blk Jun 26 '14 at 12:51

3 Answers3

3

Try adding the following to your Spring Configuration:

<context:annotation-config/>
<context:component-scan base-package="com.test.Shopping"/>
Vladimír Schäfer
  • 15,375
  • 2
  • 51
  • 71
  • nah! same exception. Anyways i have previously added annotation.AutowiredAnnotationBeanPostProcessor – aj_blk Jun 26 '14 at 12:25
  • after this edit, now exception has changed to `Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.test.Shopping.Address] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}` – aj_blk Jun 26 '14 at 12:38
  • Sorry my bad, both your changes actually worked. there was a small typo in config – aj_blk Jun 26 '14 at 12:44
1

Try to place @Qualifier on parameter rather than on method:

@Autowired
public void setDeliverydest(@Qualifier("ForPizza") Address deliverydest) { ... }
axtavt
  • 239,438
  • 41
  • 511
  • 482
1

Remove all annotation bean post processors and just add the below in xml.

<context:annotation-config/>