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

错误信息:

***************************
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. 简单推导 PCA

    考虑二维数据降低到一维的例子,如下图所示: 最小化投影方差(maximize projected variance): 1N∑n=1N(uuT1xn−uuT1x¯)=uuT1Suu1,s.t.uuT1 ...

  2. Linux性能测试 命令大全

    Linux 系统出现问题时,我们不仅需要查看系统日志信息,而且还要使用大量的性能监测工 具来判断究竟是哪一部分(内存.CPU.硬盘……)出了问题.在 Linux 系统中,所有的运行 参数保存在虚拟目录 ...

  3. 王立平--RemoteView

    RemoteView它将在两个地方被使用:一个是在AppWidget,另外一个是在Notification. RemoteView是用来描写叙述一个垮进程显示的view 1.AppWidget---R ...

  4. Python第一个基本教程4章 词典: 当指数不工作时也

    Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "copyri ...

  5. springboot 集成swagger ui

    springboot 配置swagger ui 1. 添加依赖 <!-- swagger ui --> <dependency> <groupId>io.sprin ...

  6. Ajax请求小结

    参数说明 ajax请求异步刷新页面=把需要异步刷新的页面单独写成一个.cshtml进行操作$.ajax({}); -------ajax方法.type: ------- 类型,此处为“POST” 还有 ...

  7. Go 语言如果按这样改进,能不能火过 Java?

    据 InfoWorld 消息,为改进 Go 语言的开发工具,Go 可能会获得自己的语言服务器,类似于 Microsoft 和 Red Hat 的语言服务器协议. 消息是从 Go 语言开发者的讨论组中流 ...

  8. XF 导航页面

    using System; using Xamarin.Forms; using Xamarin.Forms.Xaml; [assembly: XamlCompilation (XamlCompila ...

  9. 在mac中如何清除.svn文件

    有些时候在开发一个应用程序我们需要用到版本控制,它可以帮助我们很好的控制我们程序的代码,尤其在多人开发的时候,优点尤为突出. 但是在有些情况下我们又认为这些.svn真的很麻烦,那么我们怎么把他们一下子 ...

  10. dom写法和jquery写法

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...