8

I am getting the below exception when I am deploying the code

 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.belk.api.adapter.contract.Adapter] is defined: expected single matching bean but found 2: [endeca, solar]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:800)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    ... 64 more

I have 3 different projects one is Common, second is Adapter and the third is Service. The adapter is dependent on Common and Service is dependent on Adapter. All three are maven projects. Now in my Common project I have an interface called CommonAdapter.java

 public interface CommonAdapter {
    List service() ;
}

I have a class called AdapterFactory.java in the same project (i,e Common)

@Component
public class AdapterFactory {
    @Autowired
    Adapter adapter;
    public Adapter getAdapter(String adapterName){
        return adapter;
    }

}   
    <context:component-scan base-package="com.test.api" />
    <bean
        class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean"
        id="adapterFactory">
        <property name="serviceLocatorInterface" value="com.test.api.adapter.manager.AdapterFactory">
        </property>
    </bean>

Now in my Adapter Project, I have the implementation classes for CommonAdapter.java One is EndecaAdapetr.java and the other is SolarAdapter.java

@Component("endeca")
public class EndecaAdapter implements Adapter {
    List service() {
    // My bussiness logic
    }
}

@Component("solar")
public class SolarAdapter implements Adapter {
    List service() {
    // My bussiness logic
    }
}

Now in my Service project, want to invoke the service method of the above two classes based on the input.

public class ProductSearchServiceImpl {
    @Autowired
    private AdapterFactory adapterFactory;
    public Search searchProducts(){
    Adapter endecaAdapter = this.adapterFactory
                .getAdapter("endeca ");
 }
 }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user2334926
  • 321
  • 2
  • 7
  • 16
  • Bean names should be unique across all your configurations. It is clearly telling that no unique bean found for com.belk.api.adapter.contract.Adapter. So check your bean definitions once again. – Sivaranjani D Mar 10 '14 at 12:31

4 Answers4

12

@Autowired works only when there is no doubt on which container-managed instance should be injected. Basically, this can be attained in (at least) 3 ways:

  1. There is only one container-managed bean that IS-A declared type of the autowired field;
  2. There are more container-managed beans that validate the above IS-A condition, but the autowired field is also qualified (in Spring, by using a @Qualifier annotation)
  3. You don't use @Autowired, but rather inject the beans by name.

In your case, you have two beans that validate the IS-A condition:

endeca IS-A Adapter

and

solar IS-A Adapter.

So the container does not have a unique autowire candidate, therefore it crashes while setting up itself.

Andrei Nicusan
  • 4,555
  • 1
  • 23
  • 36
9

Use @Primary and @Resource when you have multiple implementation classes.

@Primary 
@Component("endeca")
public class EndecaAdapter implements Adapter {
    List service() {
    // My bussiness logic
    }
}

@Component("solar")
public class SolarAdapter implements Adapter {
    List service() {
    // My bussiness logic
    }
}

and inject like:

@Resource("solar")
Adapter solarAdapter;
DerMike
  • 15,594
  • 13
  • 50
  • 63
Abhishek Nayak
  • 3,732
  • 3
  • 33
  • 64
1

There are another JPA standard solution: You can use @named and @inject, so the example will be like this:

public iterface Adapter {

}

@Named
public class EndecaAdapter implements Adapter {
    List service() {
    // My bussiness logic
    }
}



@Named
public class SolarAdapter implements Adapter {
    List service() {
    // My bussiness logic
    }
}

and Inject will be like this:

@Inject @Named("SolarAdapter")
Adapter solarAdapter;

You don't need, put name, Spring do it by herself :)

-1

Already resolved here.

You must inject component with @javax.annotation.Resource(name="componentName").

Community
  • 1
  • 1
Thierry
  • 89
  • 1
  • 5