20、自动装配-@Autowired&@Qualifier&@Primary

  • 自动装配:Spring 利用依赖注入(DI),完成对IOC容器中各个依赖关系赋值

20.1 @Autowired :自动注入

  • 默认优先按照类型去容器中找对应的组件,applicationContext.getBean(BookRepository.class),找到就赋值。
  • 如果找到多个相同类型的组件,再将属性名称作为组件的id 去容器中查找applicationContext.getBean("bookRepository")
  • 使用 @Qualifier("bookRepository") 指定装配组件
  • 自动装配默认一定要将属性赋值好,没有就会报错。可以使用@Autowired(required = false)来配置非必须的注入,有则注入,没有就算了。
  • @Primary 让Spring进行自动装配的时候,默认选择需要装配的bean,也可以继续使用@Qualifier 指定需要装配的bean的名称
  • @Qualifier 的权重大于 @Primary,如果指定了@Qualifier@Primary失效

20.2 代码实例

  • MainConfigOfAutowired.java
package com.hw.springannotation.config;

import com.hw.springannotation.dao.BookRepository;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import javax.management.relation.RelationType; /**
* @Description 自动装配:
* <p>
* Spring 利用依赖注入(DI),完成对IOC容器中各个依赖关系赋值
* 1. @Autowire
* @Author hw
* @Date 2018/11/29 16:04
* @Version 1.0
*/
@Configuration
@ComponentScan({"com.hw.springannotation.service", "com.hw.springannotation.controller", "com.hw.springannotation.dao"})
public class MainConfigOfAutowired { @Primary // 首选装配bean
@Bean("bookRepository2")
public BookRepository bookRepository() {
return new BookRepository("2");
}
}
  • BookService.java
@Service
public class BookService { @Autowired(required = false)
// @Qualifier("bookRepository")
private BookRepository bookRepository2; public void print() {
System.out.println("bookRepository2。。。");
} @Override
public String toString() {
return "BookService{" +
"bookRepository=" + bookRepository2 +
'}';
}
}
  • 测试用例
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAutowired.class);

@Test
public void test1() {
BookService bookService = applicationContext.getBean(BookService.class);
System.out.println(bookService); applicationContext.close();
}

20、自动装配-@Autowired&@Qualifier&@Primary的更多相关文章

  1. 三分钟学会@Autowired@Qualifier@Primary注解

    三分钟学会@Autowired@Qualifier@Primary注解 2018.10.08 20:24 154浏览 今天主要简单的跟大家介绍一下spring自动装配相关的@Autowired,@Qu ...

  2. Spring——自动装配(@Autowired/@Profile/底层组件)

    本文介绍Spring中关于自动装配的方法和规则,以及@Profile动态激活的用法和一个例子. 一.@Autowired自动装配 @Autowired注解可以加在构造器.属性.方法.方法参数上. 自动 ...

  3. 【Spring注解驱动开发】使用@Autowired@Qualifier@Primary三大注解自动装配组件,你会了吗?

    写在前面 [Spring专题]停更一个多月,期间在更新其他专题的内容,不少小伙伴纷纷留言说:冰河,你[Spring专题]是不是停更了啊!其实并没有停更,只是中途有很多小伙伴留言说急需学习一些知识技能, ...

  4. 自动装配[@Autowired]的歧义性

    在使用@Autowired自动装配时,如果一个接口有多个实现类,那么自动装配就会出现错误,因为Spring无法判断到底要装配哪个实现类实例(bean). 1.可以使用@Qualifier(" ...

  5. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-004-消除bean自动装配的歧义@Primary

    一. 假设有如下三个类实现同一个接口,则自动装配时会产生歧义 @Component public class Cake implements Dessert { ... } @Component pu ...

  6. Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析

    一.生命周期 @Bean自定义初始化和销毁方法 //====xml方式: init-method和destroy-method==== <bean id="person" c ...

  7. 【spring 注解驱动开发】spring自动装配

    尚学堂spring 注解驱动开发学习笔记之 - 自动装配 自动装配 1.自动装配-@Autowired&@Qualifier&@Primary 2.自动装配-@Resource& ...

  8. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(10):通过注解(annotation)装配Bean之(@Configguration、@Component、@Value、@ComponentScan、@Autowired、@Primary、@Qualifier、@Bean)

    一.通过注解(annotation)装配Bean 通过之前的学习,我们已经知道如何使用XML装配Bean,但是更多的时候已经不再推荐使用XML的方式去装配Bean,更多的时候会考虑注解(annotat ...

  9. Spring注解驱动开发(三)-----自动装配

    自动装配 概念 Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值. @Autowired-----自动注入 1.默认优先按照类型去容器中找对应的组件 application ...

随机推荐

  1. 第3章:LeetCode--算法:strStr KMP算法

    https://leetcode.com/problems/implement-strstr/  28. Implement strStr() 暴力算法: int ViolentMatch(char* ...

  2. sql根据时间戳按年月日分组统计

    sql根据时间戳按年月日分组统计,用于按日期分类: create_time为时间格式(字段名create_time 根据自己表字段修改,数据库中存为201610071202) SELECT DATE_ ...

  3. 1204: 移位运算(C)

    一.题目 http://acm.wust.edu.cn/problem.php?id=1204&soj=0 二.分析 无符号短整数关键字为:unsigned short: 无符号短整数长为2字 ...

  4. c语言 c++程序运行过程

    我们写好的  .c  .cpp   文件在计算机中如何运行. 一个.c 文件  .cpp  文件  首先经过 预编译形成  . i 文件  在这个过程中 主要处理程序中的#  以及进行宏替换 然后编译 ...

  5. (七)Action之ActionContext(OGNL表达式的使用)

    一.ActionContext的重要性 struts中的数据都存放在ActionContext里,所以这部分是Action中的核心. ActionContext又称广义值栈,既然有广义值栈就有侠义值栈 ...

  6. mysql 2 修改表

    1 修改表名 rename table aaa to bbb; 或者 rename table aaa to bbb; 2 修改字段的数据类型 alter table person modify na ...

  7. 记录在苹果X手机上运行遇到的代码Dom被阻塞不更新的一个坑

    一.问题产生背景: 开发支付功能,代码逻辑如下:点击支付后,请求后台接口得到流水号以及第三方支付台链接,跳转支付台(在苹果手机则是弹出支付台层):支付完毕后返回支付页面,或中途退出支付台返回支付页面: ...

  8. stm32 按键操作

    抖动时间的长短由按键的机械特性决定,一般为5ms-10ms void key() { static u8 flag = 1; if(flag == 1 && KEY_UP == 1) ...

  9. 2019年6月SAP发布的未来ABAP平台的发展方向

    未来ABAP平台将始终是这些产品的技术平台: S/4HANA On-Premises和Cloud将基于一个统一的ABAP codeline: SAP云平台上的ABAP编程环境: 什么是SAP Clou ...

  10. SpringCloud之RabbitMQ消息队列原理及配置

    本篇章讲解RabbitMQ的用途.原理以及配置,RabbitMQ的安装请查看SpringCloud之RabbitMQ安装 一.MQ用途 1.同步变异步消息 场景:用户下单完成后,发送邮件和短信通知. ...