In the example we have currently:

@Component
public class BinarySearchImpl { @Autowired
private SortAlgo sortAlgo; public int binarySearch(int [] numbers, int target) {
// Sorting an array sortAlgo.sort(numbers);
System.out.println(sortAlgo);
// Quick sort // Return the result
return 3;
} }
@Component
@Primary
public class QuickSortAlgo implements SortAlgo{
public int[] sort(int[] numbers) {
return numbers;
}
} @Component
public class BubbleSortAlgo implements SortAlgo{
public int[] sort(int[] numbers) {
return numbers;
}
}

The way we do Autowired is by '@Primary' decorator.

It is clear that one implementation detail is the best, then we should consider to use @Primary to do the autowiring

Autowiring by name

It is also possible to autowiring by name:

// Change From
@Autowired
private SortAlgo sortAlgo; // Change to
@Autowired
private SortAlgo quickSortAlgo

We changed it to using 'quickSortAlgo', even we remove the @Primary from 'QuickSortAlgo', it still works as the same.

@Component
// @Primary
public class QuickSortAlgo implements SortAlgo{
public int[] sort(int[] numbers) {
return numbers;
}
}

@Qualifier('')

Instead of using naming, we can use @Qualifier() to tell which one we want to autowired.

@Component
@Primary
@Qualifier("quick")
public class QuickSortAlgo implements SortAlgo{
public int[] sort(int[] numbers) {
return numbers;
}
} @Component
@Qualifier("bubble")
public class BubbleSortAlgo implements SortAlgo{
public int[] sort(int[] numbers) {
return numbers;
}
}
@Autowired
@Qualifier("bubble")
private SortAlgo sortAlgo;

In this case, it will use 'BubbleSortAlgo'.

So we can say that

@Qualifier > @Primary > @naming

[Spring boot] Autowired by name, by @Primary or by @Qualifier的更多相关文章

  1. Spring Boot @Autowired 没法自动注入的问题

    Application 启动类: @SpringBootApplication @EnableConfigurationProperties @ComponentScan(basePackages = ...

  2. spring boot 2.0 提示 No primary or default constructor found for interface Pageable 解决办法

    在SpringBoot 2.0 以前,我们会配置以下类 @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter ...

  3. Spring boot @Autowired注解在非Controller中注入为null

    参考链接:https://blog.csdn.net/qq_35056292/article/details/78430777

  4. spring boot activiti 整合

    1.pom.xml <dependency> <groupId>org.activiti</groupId> <artifactId>activiti- ...

  5. [Spring Boot] @Component, @AutoWired and @Primary

    Spring boot is really good for Dependencies injection by using Autowiring. Each class instancse in s ...

  6. Spring Boot + Netty 中 @Autowired, @Value 为空解决

    问题描述 使用 Spring Boot + Netty 新建项目时 Handler 中的 @Autowired, @Value 注解的始终为空值 解决方法 @Component // 1. 添加 @C ...

  7. spring boot 如何将没有注解的类@Autowired

    等于将类交给spring管理,也就是IOC. 注解@Autowired是自动装配,也就是spring帮你创建对象,当然前提是这个@Autowired的类已经配置成Bean了,spring配置bean文 ...

  8. Spring/Spring boot正确集成Quartz及解决@Autowired失效问题

    周五检查以前Spring boot集成Quartz项目的时候,发现配置错误,因此通过阅读源码的方式,探索Spring正确集成Quartz的方式. 问题发现 检查去年的项目代码,发现关于QuartzJo ...

  9. Spring Boot 自定义 Shiro 过滤器,无法使用 @Autowired 解决方法

    在 Spring Boot 中集成 Shiro,并使用 JWT 进行接口认证. 为了统一对 Token 进行过滤,所以自定义了一个 JwtTokenFilter 过滤器. 期间遇到了以下几个问题,这里 ...

随机推荐

  1. STM32F4XX devices vector table for EWARM toolchain.

    ;/******************** (C) COPYRIGHT 2015 STMicroelectronics ******************** ;* File Name : sta ...

  2. [译] Go 并发编程基础

    原文:Fundamentals of concurrent programming 译者:youngsterxyf 本文是一篇并发编程方面的入门文章,以Go语言编写示例代码,内容涵盖: 运行期并发线程 ...

  3. Word中如何从某一页重新开始页码

  4. android 控件: xml 设置 Button 按下背景

    本篇文章讲述了不使用java代码来改变 Button 按下和未按下时的背景. 首先准备两张图片, 分别是按钮按下和按钮未按下的. 在res/drawable 文件夹中创建一个button_select ...

  5. 实现iOS长时间后台的两种方法:Audiosession和VOIP

    http://www.cocoachina.com/applenews/devnews/2012/1212/5313.html 我们知道iOS开启后台任务后可以获得最多600秒的执行时间,而一些需要在 ...

  6. WordPress主题开发:循环代码

    have_posts() 有没有文章信息 if...else <?php if( have_posts() ) : while( have_posts() ) : the_post(); ?&g ...

  7. Unity Shader-渲染队列,ZTest,ZWrite,Early-Z

    在渲染阶段,引擎所做的工作是把所有场景中的对象按照一定的策略(顺序)进行渲染.最早的是画家算法,顾名思义,就是像画家画画一样,先画后面的物体,如果前面还有物体,那么就用前面的物体把物体覆盖掉,不过这种 ...

  8. fastjson转换对象,属性首字母大小写的问题

    请求Json数据的时候,传递过去的String类型转Json数据的时候经常有首字母是大写的情况,例如"LoginAccount":"02:00:00:62:73:74&q ...

  9. 混乱之子第一季/全集Sons Of Anarchy迅雷下载

    本季第一至六季 Sons of Anarchy (2008-2013)看点:<混乱之子>发生在一个虚构的加州小镇(Charming)上,面对毒品贩子和大型土地开发商的步步紧逼,一家黑白两道 ...

  10. Java并发编程的艺术(十三)——锁优化

    自旋锁 背景:互斥同步对性能最大的影响是阻塞,挂起和恢复线程都需要转入内核态中完成:并且通常情况下,共享数据的锁定状态只持续很短的一段时间,为了这很短的一段时间进行上下文切换并不值得. 原理:当一条线 ...