java - No unique bean of type is defined: expected single matching bean but found 2 -
this question has answer here:
i getting below exception when deploying code
caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no unique bean of type [com.belk.api.adapter.contract.adapter] defined: expected single matching bean found 2: [endeca, solar] @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:800) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:707) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:478) ... 64 more
i have 3 different projects 1 common, second adapter , third service. adapter dependent on common , service dependent on adapter. 3 maven projects. in common project have interface called commonadapter.java
public interface commonadapter { list service() ; }
i have class called adapterfactory.java in 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 adapter project, have implementation classes commonadapter.java 1 endecaadapetr.java , other solaradapter.java
@component("endeca") public class endecaadapter implements adapter { list service() { // bussiness logic } } @component("solar") public class solaradapter implements adapter { list service() { // bussiness logic } }
now in service project, want invoke service method of above 2 classes based on input.
public class productsearchserviceimpl { @autowired private adapterfactory adapterfactory; public search searchproducts(){ adapter endecaadapter = this.adapterfactory .getadapter("endeca "); } }
@autowired
works when there no doubt on container-managed instance should injected. basically, can attained in (at least) 3 ways:
- there 1 container-managed bean is-a declared type of autowired field;
- there more container-managed beans validate above is-a condition, autowired field qualified (in spring, using
@qualifier
annotation) - you don't use
@autowired
, rather inject beans name.
in case, have 2 beans validate is-a condition:
endeca
is-a adapter
and
solar
is-a adapter
.
so container not have unique autowire candidate, therefore crashes while setting itself.
Comments
Post a Comment