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. Software UART, Timer, PWM, External Interrupt

    How can you add extra hardware UARTs to a 32bit TMS470 ARM7-based microcontroller at zero cost? Solu ...

  2. C#打印图片

    打印的原理是:生成mdi文件,系统碰到mdi的时候会自动以打印的方式处理.所以,不管用什么模板,什么方式:能在PrintPage事件处理中,生成一张要打印内容的图片就OK了! C#实现打印源码如下: ...

  3. JavaScript学习总结(十五)——Function类

    在JavaScript中,函数其实是对象,每个函数都是Function类的实例,既然函数对象,那么就具有自己的属性和方法,因此,函数名实际上也是一个指向函数对象的指针,不会与某个函数绑定. 一.函数的 ...

  4. 使用Axure RP原型设计实践07,注册判断

    本篇实现注册页的一些功能.本项目是通过用户名和电子邮件进行注册的. 在本篇之前,在"使用Axure RP原型设计实践03,制作一个登录界面的原型"中已经对注册页做了基本的处理. 打 ...

  5. 多个按钮触发同一个Bootstrap自适应模态窗口

    在项目中可能会面对这样的一个场景: 界面上有多个按钮,我们希望点击这些按钮弹出同一个模态窗口,但希望模态窗口的内容是动态生成的,即,点击每个按钮弹出的模态窗口内容不同. 通常情况下,一个按钮对应一个模 ...

  6. UIView 的旋转和缩放

    原文地址:http://www.cnblogs.com/gaoxiao228/archive/2012/05/04/2483577.html label.transform = CGAffineTra ...

  7. arcgispro字段计算器

    使用python语法 在python中没有类似sub()或者subString()的方法,但是字符串的截取操作却是更加简单. 只需要把字符串看作是一个字符数组,截取子串非常方便. 多余的话就不啰嗦了, ...

  8. 两个Activity之间共享数据、互相访问的另一种方式的实现

    本帖最后由 勇敢的心_ 于 2010-9-29 11:51 编辑 本人从windows编程转过来学习Android开发,一直在想如果两个Activity之间能够像C#或delphi中的Form一样,可 ...

  9. byte[]数组的正则表达式搜索 z

    在byte[]数组的特定位置进行正则表达式匹配. 为了从硬盘上搜索特定类型的文件,需要根据文件的特征值进行匹配. 对于已掌握文件结构的文件,采用hard-code的方式进行匹配:这样速度快: 对于未掌 ...

  10. cloudera项目源代码

    以下项目都需要安装git,Linux的git还是比较容易安装的,windows的git安装参考项目区域:软件版本控制-在Windows中使用Git视频介绍 git相关软件安装参考win7安装 git软 ...