【记录】Field required a single bean, but 2 were found:
重构遇到个小问题,记录下:
错误信息:
***************************
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:的更多相关文章
- 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 ...
- Field in required a single bean, but 2 were found:
我在其他类注入的时候出现以下错误 @Autowired NodeAgentService nodeAgentService; 异常 Description: Field mibService in c ...
- 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 ...
- @Autowired注解 --required a single bean, but 2 were found出现的原因以及解决方法
@Autowired注解是spring用来支持依赖注入的核心利器之一,但是我们或多或少都会遇到required a single bean, but 2 were found(2可能是其他数字)的问题 ...
- 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, ...
- springboot多数据源启动报错:required a single bean, but 6 were found:
技术群: 816227112 参考:https://stackoverflow.com/questions/43455869/could-not-autowire-there-is-more-than ...
- Spring多个数据源问题:DataSourceAutoConfiguration required a single bean, but * were found
原因: @EnableAutoConfiguration 这个注解会把配置文件号中的数据源全部都自动注入,不会默认注入一个,当使用其他数据源时再调用另外的数据源. 解决方法: 1.注释掉这个注解 2. ...
- 【单条记录锁】select single for update
示例: DATA: wa_t001 type t001. select single for update * into wa_t001 from t001 where bukrs = '1000'. ...
- Spring学习记录(十)---使用FactoryBean配置Bean
之前学了,配置bean可以用普通全类名配置.用工厂方法配置,FactoryBean又是什么呢 有时候配置bean要用到,IOC其他Bean,这时,用FactoryBean配置最合适. FactoryB ...
随机推荐
- Java transient关键字【转】
转自:http://www.blogjava.net/fhtdy2004/archive/2009/06/20/286112.htmlVolatile修饰的成员变量在每次被线程访问时,都强迫从主内存中 ...
- ORA-00600: internal error code, arguments: [4194], [53], [41], [], [], [], [], []
真的不动,关闭同事开发测试IBM 3650server它直接关系到电源插头行??? 第二天加点重新启动之后oracle 打开报错ORA-00600: internal error code, argu ...
- Gtk+/Gtkmm介绍与安装(有继承关系图)
GTK+是一套跨平台的,以C语言编写的GUI类库,它起源于GNU的图像处理程序"GIMP",因而被起名为"The GIMP Toolkit",从一开始它就被设计 ...
- Linux 系统安装(5分钟)
安装版本:CentOS 6.5 minimal 虚拟机工具:VMware 虚拟机配置:1核2线程 2G内存 50G硬盘 步骤: 一.虚拟机配置 1.打开VMware,创建新的虚拟机,选择典型安装: 2 ...
- HierarchicalDataTemplate
针对具有分层数据结构的控件设计的,比如说TreeView,相当于可以每一个层级上做DataTemplate XmlDataProvider:数据源,写在Resources下 <XmlDataPr ...
- 一个RPC的demo (good)
从下面的例子中可以看到,Consumer(client)的代码中引用了Provider部分的class,本例中是 com.provider.EchoServiceImpl和com.provider.E ...
- Wow6432Node
64 位版本 Windows 中的注册表分为 32 位注册表项和 64 位注册表项.许多 32 位注册表项与其相应的 64 位注册表项同名,反之亦然. 64 位版本 Windows 包含的默认 64 ...
- C#数字图像处理时注意图像的未用区域
原文:C#数字图像处理时注意图像的未用区域 图1. 被锁定图像像素数组基本布局 如图1所示,数组的宽度并不一定等于图像像素数组的宽度,还有一部分未用区域.这是为了提高效率,系统要确定每 ...
- Win10《芒果TV》更新v3.8.50勇敢版:新增短信和扫码登录
勇敢,是心中最初的信仰,实景科幻实验节目<勇敢的世界>,重装上阵对抗升级,<中餐厅2>皇阿玛圣驾亲临,坐镇中国味道.Win10版<芒果TV>全平台同步更新勇敢版v3 ...
- Python杂谈: __init__.py的作用
我们经常在python的模块目录中会看到 "__init__.py" 这个文件,那么它到底有什么作用呢? 1. 标识该目录是一个python的模块包(module package ...