Spring为什么@Autowired注入的是接口
1.Spring怎么知道注入哪个实现?
As long as there is only a single implementation of the interface and that implementation is annotated with @Component with Spring’s component scan enabled, Spring framework can find out the (interface, implementation) pair. If component scan is not enabled, then you have to define the bean explicitly in your application-config.xml (or equivalent spring configuration file).
如果Spring配置了component scan,并且要注入的接口只有一个实现的话,那么spring框架可以自动将interface于实现组装起来。如果没有配置component scan,那么你必须在application-config.xml(或等同的配置文件)定义这个bean。
2.需要@Qualifier和@Resource注解吗?
Once you have more than one implementation, then you need to qualify each of them and during auto-wiring, you would need to use the @Qualifier annotation to inject the right implementation, along with @Autowired annotation. If you are using @Resource (J2EE semantics), then you should specify the bean name using the name attribute of this annotation.
一旦一个接口有多个实现,那么就需要每个特殊化识别并且在自动装载过程中使用@Qualifier和@Autowired一起使用来标明。如果是使用@Resource注解,那么你应该使用resource中属性名称来标注@Autowired.
Spring为什么@Autowired注入的是接口的更多相关文章
- 【Intellij idea】spring中@Autowired注入失败
@Autowired注入失败失败的解决办法? 现有的解决的方案是: 打开file-settings或者ctrl+alt+s -> Editor 然后在Inspections 点击搜索栏 输入Sp ...
- 当spring 对象@Autowired 注入失败或者创建对象Bean失败、No qualifying bean/Error creating bean 的失败情形分析和解决方案
错误信息 今天开发的过程中突然出现如下错误: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: N ...
- 实现Callable的对象中,用@Autowired注入别的对象失败
场景是这样: 我需要在一个实现类A中写一个拿到返回值的多线程,于是用的Callable,在这个实现类A外我又写了一个专门实现Callable的实现类B,在B中用spring注解@Autowired注入 ...
- 解决 spring boot 线程中使用@Autowired注入Bean的方法,报java.lang.NullPointerException异常
问题描述 在开发中,因某些业务逻辑执行时间太长,我们常使用线程来实现.常规服务实现类中,使用 @Autowired 来注入Bean,来调用其中的方法.但如果在线程类中使用@Autowired注入的Be ...
- Spring IOC三种注入方式(接口注入、setter注入、构造器注入)(摘抄)
IOC ,全称 (Inverse Of Control) ,中文意思为:控制反转, Spring 框架的核心基于控制反转原理. 什么是控制反转?控制反转是一种将组件依赖关系的创建和管理置于程序外部的技 ...
- IntelliJ IDEA中Mapper接口通过@Autowired注入报错的正确解决方式
转载请注明来源:四个空格 » IntelliJ IDEA中Mapper接口通过@Autowired注入报错的正确解决方式: 环境 ideaIU-2018.3.4.win: 错误提示: Could no ...
- Quartz定时器+Spring + @Autowired注入 空指针异常
在Quartz的定时方法里引用@Autowired注入Bean,会报空指针错误 解决办法: 第一种方法:(推荐,简单,亲测可行) 使用@Resource(name="指定要注入的Bean&q ...
- new出来的对象无法调用@Autowired注入的Spring Bean
@Autowired注入Spring Bean,则当前类必须也是Spring Bean才能调用它,不能用new xxx()来获得对象,这种方式获得的对象无法调用@Autowired注入的Bean. 1 ...
- spring boot开发 @autowired注入失败
@autowired注入失败 会出现如下错误提示: 2018-05-28 08:39:41.857 INFO 8080 --- [ restartedMain] org.hibernate.Versi ...
随机推荐
- 美化WebApi,使其统一返回Json格式
博客部分代码来自其他博主,暂时找不到你的博文连接,如果您觉得我的代码中引入了您的代码或者文章,可在下方把您的博客文章写在下面,谢谢!!! WebApi有两种返回数据格式,一种是XML,一种是Json, ...
- cuda环境搭建
cuda环境搭建 cuda 的安装 一篇很不错的博客 https://blog.csdn.net/u014529295/article/details/78766258 另外官网也有介绍 https: ...
- IDEA实用教程(二)
2. 基础设置 1) 进入全局设置 2) 更改主题 3) 修改主题字体 4) 修改代码编辑区字体 5) 修改控制台字体 图中3处修改控制台字体 图中4处修改控制台字体 6) 文件编码的设置 图中4处建 ...
- linux网络编程之posix消息队列
在前面已经学习了System v相关的IPC,今天起学习posix相关的IPC,关于这两者的内容区别,简单回顾一下: 而今天先学习posix的消息队列,下面开始: 接下来则编写程序来创建一个posix ...
- DEVC++如何调试代码
DEVC++小技巧 学习C语言的同学大多都会使用DEVC++这个软件,但是在使用的时候会发现是不可以调试的,因为我们的软件默认是将调试关闭了的.下面是调试的具体方法. 点击窗口的工具按钮 点击编辑按钮 ...
- js onclick事件传参
传字符串参数 var html = "<a href='#' onclick='onedit(""+ row.name + "")';>编 ...
- 【轉】mantis安裝
一.mantis简介 可以看出,mantis是一个基于php技术的,个人觉得这个系统还是很完善的. 安装mantis,需要安装一下软件: phpMyAdmin 下载地址https://w ...
- CSS float详解
前言:在我们写CSS样式的时候,float,position,display,overflow这几个关键字用得比较多. 弄清楚他们之间的原理,我们可以更高效的写出我们想要的布局. 作者:Ry-yuan ...
- 编写一个c程序来计算整数中的设置位数?
回答: unsigned int NumberSetBits(unsigned int n) { ; while (n) { ; ; } return CountSetBits; } 本质上就是计算n ...
- CSP模拟赛 number (二分+数位DP)
题面 给定整数m,km,km,k,求出最小和最大的正整数 nnn 使得 n+1,n+2,-,2nn+1,n+2,-,2nn+1,n+2,-,2n 中恰好有 mmm 个数 在二进制下恰好有 kkk 个 ...