默认根据类型,匹配不到则根据bean名字

1.声明一个service接口

public interface HelloService {
void sayHello();
}

2.service接口的实现类,此时bean名字是 helloServiceImpl

@Service
public class HelloServiceImpl implements HelloService {
@Override
public void sayHello() {
System.out.println("say hello impl");
}
}

3.增加一个Controller,注入service

// 生成一个bean,名字为 helloController
@Controller
public class HelloController {
@Autowired
private HelloService helloService; public void hello() {
helloService.sayHello();
}
}

4.测试①:

public class AppTest {

    public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloController controller = (HelloController) context.getBean("helloController");
controller.hello();
}
}

结果如下

成功将Service层的实现类注入到Controller层中,可以把步骤3 代码修改一下

// 生成一个bean,名字为 helloController
@Controller
public class HelloController {
@Autowired
private HelloService abc; public void hello() {
abc.sayHello();
}
}

结果也是可以的,因为@Autowired 第一是按照类型去匹配的,此时IoC容器中HelloService 接口只有一个实现类,所以属性名字怎么写都没关系,都可以注入进去

测试②:增加一个实现类,此时bean名字是 newServiceImpl

@Service
public class NewHelloServiceImpl implements HelloService {
@Override
public void sayHello() {
System.out.println("new say hello impl");
}
}

现在IoC容器中有两个 HelloService接口的实现类,继续运行测试方法,结果为

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'helloController':
Unsatisfied dependency expressed through field 'abc';
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'com.convict.service.HelloService' available:
expected single matching bean but found 2: helloServiceImpl,newHelloServiceImpl

因为一个接口有多个实现,所以@Autowired 就按照属性名字去找,即找一个名字为 abc的bean注入,然而IoC容器不存在一个名字叫abc的 bean,因此报错,把属性名改为下面任意一种就可以匹配到了

// 生成一个bean,名字为 helloController
@Controller
public class HelloController {
@Autowired
private HelloService helloServiceImpl; @Autowired
private HelloService newHelloServiceImpl; public void hello() {
helloServiceImpl.sayHello();
newHelloServiceImpl.sayHello();
}
}

测试③:

那我就要把属性名叫 abc,同时有多个实现,而且还能注入,那么在声明组件的时候取个名字就好了,比如

@Service("abc")
public class HelloServiceImpl implements HelloService {
@Override
public void sayHello() {
System.out.println("say hello impl");
}
}

然后Controller 注入的还是abc,结果注入成功

// 生成一个bean,名字为 helloController
@Controller
public class HelloController {
@Autowired
private HelloService abc; public void hello() {
abc.sayHello();
}
}

测试④:

属性名叫 abc,同时有多个实现,同时可以注入,且不在注解处声明bean 的名字,那么这时候使用新的注解@Qualifier 配合@Autowired 一起使用

// 生成一个bean,名字为 helloController
@Controller
public class HelloController {
@Autowired
@Qualifier("helloServiceImpl")
private HelloService abc; public void hello() {
abc.sayHello();
}
}

@Qualifier是指定 一个bean的名字

总结:

1.一个接口只有一个实现的情况下,属性名字怎么写都无所谓,因为按照类型匹配就只有一个bean

2.一个接口多个实现的情况下:

  ① 属性名字跟组件名字一致,组件名字可以在声明的时候指定,比如 @Service("abc")

  ② 属性名字跟组件名字不一致,配合@Qualifier 注解指定组件名字

Spring中@Autowired 注解的注入规则的更多相关文章

  1. Spring中@Autowired注解、@Resource注解的区别 (zz)

    Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...

  2. Spring中@Autowired注解与自动装配

    1 使用配置文件的方法来完成自动装配我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. ...

  3. Spring中Autowired注解,Resource注解和xml default-autowire工作方式异同

    前面说到了关于在xml中有提供default-autowire的配置信息,从spring 2.5开始,spring又提供了一个Autowired以及javaEE中标准的Resource注释,都好像可以 ...

  4. Spring中 @Autowired注解与J2EE@Resource注解的区别

    在开发中经常使用到@Autowired和@Resource进行装配. 不禁好奇这两个注解的差异在何处??? 相同点: @Resource的作用相当于@Autowired,均可标注在字段或属性的sett ...

  5. Spring中@Autowired注解、@Resource注解的区别

    Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...

  6. 转:Spring中@Autowired注解、@Resource注解的区别

    Pay attention: When using these annotations, the object itself has to be created by Spring context. ...

  7. Spring中的注解配置-注入bean

    在使用Spring框架中@Autowired标签时默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个.当找不到一个匹配的 Bean ...

  8. @Resource或者@Autowired作用/Spring中@Autowired注解、@Resource注解的区别

    @Resource或者@Autowired作用不用写set get就能注入,当然,前提是你已经开启了注解功能. spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定 ...

  9. Spring中 @Autowired注解与@Resource注解的区别

    Spring中 @Autowired注解与@Resource注解的区别在Spring 3.X中经常使用到@Autowired和@Resource进行装配.这两个注解的差异在何处???相同点:@Reso ...

随机推荐

  1. CSS基础 背景图片的相关属性

    属性名: background-size: 宽度 高度; 属性值 说明 数字+px 简单方便,常用 百分比 相当于盒子自身的百分比,如:百分百,就是就算是图片变形也要显示 contain 动比例缩放, ...

  2. Window10系统修改hosts文件的方法

    背景: 调试smtp程序时遇到问题,度娘说需要修改hosts文件 使用老方法修改了很久,始终无法保存 又百度了一下,在此重温,以加深记忆 方法: Step1.同时按住Windows+X Step2.选 ...

  3. unittest_expectedFailure预期用例失败(5)

    在断言用例执行结果时,会出现预期结果与实际结果不一致的情况,此时我们明确知道用例执行结果为FAIL,不想看到打印错误信息怎么办? 使用装饰器@unittest.expectedFailure标记该用例 ...

  4. spring boot 解决 跨域 的两种方法 -- 前后端分离

    1.前言 以前做项目 ,基本上是使用 MVC 模式 ,使得视图与模型绑定 ,前后端地址与端口都一样 , 但是现在有些需求 ,需要暴露给外网访问 ,那么这就出现了个跨域问题 ,与同源原则冲突, 造成访问 ...

  5. atroot 的个人博客

    我的个人博客 左上角 MENU 打开导航菜单! 向下滚动查看内容! 为啥我要坚持更新博客 周围有很多小伙伴在问,你写博客会有人看嘛?如果没人看,那岂不是写的就没有意义了吗? 这个问题也一度让我陷入是否 ...

  6. 闯祸了,生成环境执行了DDL操作《死磕MySQL系列 十四》

    由于业务随着时间不停的改变,起初的表结构设计已经满足不了如今的需求,这时你是不是想那就加字段呗!加字段也是个艺术活,接下来由本文的主人咔咔给你吹. 试想一下这个场景 事务A在执行一个非常大的查询 事务 ...

  7. Sentry 开发者贡献指南 - Feature Flag

    功能 flag 在 Sentry 的代码库中声明. 对于自托管用户,这些标志然后通过 sentry.conf.py 进行配置. 对于 Sentry 的 SaaS 部署,Flagr 用于在生产中配置标志 ...

  8. CSS-选择器的使用

    * 默认选择器,这个符号能匹配所有样式,所以如果没有额外定义就默认为这个样式,一般用于消除页面与浏览器的内外边距 <style> *{ padding:0; // 所有标签默认消除内边距 ...

  9. [USB波形分析] 全速USB波形数据分析(二)

    在上一篇文章全速USB波形数据分析(一)介绍了全速USB的数据包(Packet)的组成,数据的类型等基本知识.这篇文章介绍USB的几种传输方式 事务(Transaction) USB协议定义了三种不同 ...

  10. i-Urban Renovation使用3D Tiles可视化鸟取县Munakata建筑状态

    Cesium中文网:http://cesiumcn.org/ | 国内快速访问:http://cesium.coinidea.com/ 日本的鸟取县,使用i-Urban Renovation appl ...