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. HTTP最常见的请求头

    HTTP最常见的请求头如下: l         Accept:浏览器可接受的MIME类型: l         Accept-Charset:浏览器可接受的字符集: l         Accept ...

  2. PAT甲级 图的遍历 相关题_C++题解

    图的遍历 PAT (Advanced Level) Practice 图的遍历 相关题 目录 <算法笔记>重点摘要 1021 Deepest Root (25) 1076 Forwards ...

  3. 在订单服务中使用Hystrix进行熔断设置

    使用Hystrix熔断(上) 在一个分布式系统里,一个服务依赖多个服务,可能存在某个服务调用失败,         比如超时.异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,   ...

  4. 开启 oracle 的闪回功能

    查看是否开启闪回 SQL> select flashback_on from v$database; FLASHBACK_ON ------------------ NO 查看是否配置了db_r ...

  5. php 配置微信公众号

    首先你要在微信公众号官网申请一个公众号,然后登录进去 在网页的左下方找到开发的基本配置 就可以开始配置服务器下面的东西了 点击打开成这个样子的 就是一些参数,url填写你自己服务器的具体地址就好了,我 ...

  6. [异步请求]ajax、axios、fetch之间的详细区别以及优缺点

    1.jQuery ajax  $.ajax({ type: 'POST', url: url, data: data, dataType: dataType, success: function () ...

  7. sql注入测试(2)---实例测试

    以下篇幅,用一个简单的实例说明如何进行测试. 功能:根据用户NAME删除用户,采用的是SQL拼接的方式,核心代码部分如下: public static void deleteByName(String ...

  8. (错误)启动ActiveMQ报错:Transport Connector could not be registered in JMX: java.io.IOException: Failed to bind to server socket: stomp://0.0.0.0:61613?

    一.错误报告 很明显,端口被占用 二.解决方法 1. 在cmd中输入 netstat -ano 查看61613端口被占用情况,如果有其他进程使用,则使用 taskkill /f /pid 进程PID  ...

  9. Mysql对查询结果添加序列号

    ; as rownum,* from table1 :=和=的区别? "=",只有在set和update时才和:=一样,赋值的作用,其它都是等于的作用.鉴于此,用变量实现行号时,必 ...

  10. Mysql 更新时间

    Mysql时间加减函数为date_add().date_sub() 定义和用法DATE_ADD() 函数向日期添加指定的时间间隔.DATE_SUB() 函数向日期减少指定的时间间隔.语法DATE_AD ...