问题:
org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type [main.service.Singer] is defined: expected single matching bean but found 2: metalSinger,operaSinger
或者:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
如:
public interface StringHandler;
@Service
public ToUpperImpl implentments StringHandler {}
@Service
public ToLowerImpl implentments StringHandler {}
解决方案:
1> 使用@Resource(name=“toLowerImpl ”) 注解指定引用类名(默认首字母小写)
2>使用@Autowired @Qualifier("toLowerImpl") 组合注解指定应用类名(默认首字母小写:可修改->@Service("指定名称"))
3>使用@Primary 注解在指定的service实现上,告诉spring 在多个实现的情况,哪一个首要使用
4>spring4.x后,@Conditional设定某个实现类引用的条件,可自行研究,此处不做详解..
其他方式,可自行尝试,或可交流