解决多实现引用的问题

来源:互联网 发布:淘宝店运营方案怎么写 编辑:程序博客网 时间:2024/06/01 19:40

问题:

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设定某个实现类引用的条件,可自行研究,此处不做详解..

其他方式,可自行尝试,或可交流

原创粉丝点击