【记录】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 ...
随机推荐
- Entity Framework 6 编译出错的问题(VS2012)
更新:其实这个问题是由于VS2012的EF代码生成模板是EF 5.x的,自然会与EF6 的runtime不兼容.起初我按照更新前的方式解决了,后来却发现会出现不止这一处命名空间发生改动而导致的问题. ...
- WPF实现抽屉效果
原文:WPF实现抽屉效果 界面代码(xaml): <Window x:Class="TransAnimation.MainWindow" xmlns="http:/ ...
- 你所不知道的 Kindle - 阅读微信公众号文章
Kindle 是一款非常优秀的阅读设备,它为我们提供了非常舒服的阅读体验,并且配合强大的亚马逊图书资源,应该是目前最好的阅读设备之一.Kindle 在已有的成就下还一直在努力提升用户体验.为中国用户开 ...
- 日志文件 清理or压缩
1.操作前请断开所有数据库连接. 2.分离数据库 分离数据库:企业管理器->服务器->数据库->cwbase1->右键->分离数据库 分离后,cwbase1数据库被删除, ...
- jquery 访问cookie
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- WPF值转换器的使用
<Window x:Class="CollectionBinding.MainWindow" xmlns="http://schemas.micros ...
- QT pro 添加带空格的路径以及添加库文件的正确方法
用这个:$$quote() 如何添加库?看下面添加mysql路径的例子: INCLUDEPATH += $$quote(C:\Program Files (x86)\MySQL\MySQL Serve ...
- 【C#】解决MouseHook捕获鼠标动作,在有些电脑上SetWindowsHookEx失败返回0的问题
原文:[C#]解决MouseHook捕获鼠标动作,在有些电脑上SetWindowsHookEx失败返回0的问题 最近在debug鼠标位置捕获的功能时发现在其中的一台开发电脑上,SetWindowsHo ...
- python 运行出现flask运行时提示出错了或者报服务器出错,ValueError: View function did not return a response
python manage.py runserver -d
- WinForm TreeView单击,但是获取的是上一次点击选中的节点
/// <summary> /// MouseDown是鼠标按下事件发生在你鼠标单击事件之前,你单击鼠标发生了两个动作,一是鼠标按下二是鼠标抬起.执行之后,就会把SelectedNode转 ...