重构遇到个小问题,记录下:

错误信息:

***************************
APPLICATION FAILED TO START
*************************** Description: Field xxxService in com.alibaba.xxx required a single bean, but 2 were found:

解决方法:

Action:

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

代码案例:

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; /**
* 一个接口
*/
public interface DataTransferService {
Long transferData() throws Exception; } /**
* 实现类一
* 重要:添加@Primary标记为默认初始化的类
*/
@Component("dataTransferService")
@Primary
class DataTransferServiceImpl implements DataTransferService { @Override
public Long transferData() throws Exception {
return 0L;
}
} /**
*实现类二
*/
@Component("dataTransferServiceSync")
class DataTransferServiceSyncImpl implements DataTransferService { @Override
public Long transferData() throws Exception { return 0L;
}
}

使用方式:

/**
* 两种使用方式,已测试
*/
public class ActivityDemo {
/**方式一*/
@Autowired
@Qualifier("dataTransferService")
protected DataTransferService dataTransferService; @Autowired
@Qualifier("dataTransferServiceSync")
protected DataTransferService dataTransferServiceSync; /**方式二*/ // @Resource("dataTransferService")
// protected DataTransferService dataTransferService;
//
// @Resource("dataTransferServiceSync")
// protected DataTransferService dataTransferServiceSync;
}

注意:添加@Primary告诉spring初始化时使用哪个主要的实现类。

补充:https://baijiahao.baidu.com/s?id=1608114169828948852&wfr=spider&for=pc

@Autowired与@Resource的区别

(1)@Autowired

@Autowired为Spring提供的注解,需要导入包org.springframework.beans.factory.annotation.Autowired;只按照byType注入。

@Autowired注解是按照类型(byType)装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它的required属性为false。如果我们想使用按照名称(byName)来装配,可以结合@Qualifier注解一起使用。如下:

(2)@Resource

@Resource默认按照ByName自动注入,由J2EE提供,需要导入包javax.annotation.Resource。@Resource有两个重要的属性:name和type,而Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。

所以,如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不制定name也不制定type属性,这时将通过反射机制使用byName自动注入策略。

【记录】Field required a single bean, but 2 were found:的更多相关文章

  1. Field amqpTemplate in * required a single bean, but 3 were found:

    Field amqpTemplate in * required a single bean, but 3 were found: Spring Boot 启动的时候报的错 使用Spring Boot ...

  2. Field in required a single bean, but 2 were found:

    我在其他类注入的时候出现以下错误 @Autowired NodeAgentService nodeAgentService; 异常 Description: Field mibService in c ...

  3. Field baseMapper in com.baomidou.mybatisplus.extension.service.impl.ServiceImpl required a single bean, but xx were found:

    在学习使用 mybatis-plus 时,遇到一个奇怪的异常 如 代码一: 代码一: Error starting ApplicationContext. To display the conditi ...

  4. @Autowired注解 --required a single bean, but 2 were found出现的原因以及解决方法

    @Autowired注解是spring用来支持依赖注入的核心利器之一,但是我们或多或少都会遇到required a single bean, but 2 were found(2可能是其他数字)的问题 ...

  5. Parameter 0 of method sqlSessionTemplate in org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration required a single bean, but 2 were found:

    Parameter 0 of method orderSqlSessionFactory in com.config.MultipleDBConfig required a single bean, ...

  6. springboot多数据源启动报错:required a single bean, but 6 were found:

    技术群: 816227112 参考:https://stackoverflow.com/questions/43455869/could-not-autowire-there-is-more-than ...

  7. Spring多个数据源问题:DataSourceAutoConfiguration required a single bean, but * were found

    原因: @EnableAutoConfiguration 这个注解会把配置文件号中的数据源全部都自动注入,不会默认注入一个,当使用其他数据源时再调用另外的数据源. 解决方法: 1.注释掉这个注解 2. ...

  8. 【单条记录锁】select single for update

    示例: DATA: wa_t001 type t001. select single for update * into wa_t001 from t001 where bukrs = '1000'. ...

  9. Spring学习记录(十)---使用FactoryBean配置Bean

    之前学了,配置bean可以用普通全类名配置.用工厂方法配置,FactoryBean又是什么呢 有时候配置bean要用到,IOC其他Bean,这时,用FactoryBean配置最合适. FactoryB ...

随机推荐

  1. UWP 中的各种文件路径(用户、缓存、漫游、安装……)

    原文 UWP 中的各种文件路径(用户.缓存.漫游.安装……) UWP 提供了多种不同文件路径访问方式,对应到不同的文件路径中.可能我们只是简单用 ApplicationData.Current 获取一 ...

  2. Python算法(含源代码下载)

    关键字:Python Algorithms Python算法  Mastering Basic Algorithms in the Python Language 使用Python语言掌握基本算法 P ...

  3. [OpenGL]OpenGL坐标系和坐标变换

    OpenGL通过摄像机的模拟.要实现一个三维计算机图形重大转变,这是几何变换(模型转换-查看转型(两者统称为几何变换)).投影.作物转型.口变换等.同一时候,OpenGL还实现了矩阵堆栈等.理解掌握了 ...

  4. Python3获取股票行情数据(中国个股/中国指数/全球指数)

    #!/usr/local/bin/python3 #coding=utf-8 #source http://www.cnblogs.com/txw1958/ import os, io, sys, r ...

  5. Open SSL 开发环境配置

    Open SSL 开发环境配置 最后更新日期:2014-05-13 阅读前提:VisualStudio的基本使用.Cent OS的基本使用 环境: Windows 8.1 64bit英文版,Visua ...

  6. HDU5187 zhx's contest(计数问题)

    主题链接: http://acm.hdu.edu.cn/showproblem.php?pid=5187 题意: 从1~n,有多少种排列 使得 a1~ai 满足单调递增或者单调递减. ai~an 满足 ...

  7. qt4.8.7 源码在win7+vs2010环境的x64编译(qt 64位)

    由于qt官网上,没有直接提供x64的安装包,但由于项目需要x64的qt,所以,小编不得不下载qt的源码,经历了一次长达约4个小时的编译过程.今年国庆7天,就遭这事上了,哈哈~~~ 几个下载链接: qt ...

  8. google 搜索结果在新标签中打开

    google->setting->search setting->Where results open->检查

  9. Emgu-WPF学习使用-Rectangle识别

    原文:Emgu-WPF学习使用-Rectangle识别 环境:Win8 64位 Vs2015 Emgu 版本:emgucv-windesktop 3.2.0.2682 示例图上部流程:原图->灰 ...

  10. js 生成表格及其颜色

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...