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 ...
随机推荐
- 【数组模拟-小顶堆的插入构造/遍历】PAT-L2-012.-关于堆的判断--数组模拟
L2-012. 关于堆的判断 将一系列给定数字顺序插入一个初始为空的小顶堆H[].随后判断一系列相关命题是否为真.命题分下列几种: “x is the root”:x是根结点: “x and y ar ...
- P1462 通往奥格瑞玛的道路[最短路+二分+堆优化]
题目来源:洛谷 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描 ...
- 接口调优——WebAPI 过滤器,IIS WebDAV
目录 1.身份认证过滤器—AuthenticationFilter 2.Action 过滤器—ActionFilter 3.异常处理过滤器—ExceptionFilterAttribute 4.IS ...
- 为RIDE创建桌面快捷方式
问题场景:默认情况下,RIDE的图标不是自动创建的,需要手动添加. 解决方法: 在桌面上新建"快捷方式" 目标对象的位置:C:\Python27\python2.exe - ...
- 【bzoj3238】差异 后缀树
题目大意:给你一个字符串$S$,设$S_i$是串$S$第$i$长的后缀,求: $\sum\limits_{i=1}^{|S|} \sum\limits_{j=i+1}^{|S|} |S_i|+|S_j ...
- python 杂记-unittest
介绍单元测试的好文:https://mp.weixin.qq.com/s/njxc8GXSlc3z_RibK70ROg setUpModule/tearDownModule:在整个模块的开始和结束时被 ...
- js事件冒泡/捕获
- 定时器 间隔调用setInterval
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SqlProfilter监控指定数据库数据表——监控linq组合查询生成的sql
1.例子 实际测试环境中往往很多测试都在调用数据库,那么如何使用SqlProfilter监控筛选到自己想要监看的数据库对应的表有关linq生成的sql时候就需要做如下设置了 ........... u ...
- easyui--权限管理
1.权限目的: 是为了让不同的用户可以操作系统中不同资源 直接点说就是不同的用户可以操作不同的菜单 核心:实现菜单权限的核心思想就是控制用户登录后台所传递的menuId(与树形菜单分类列段 ...