[Spring boot] Autowired by name, by @Primary or by @Qualifier
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的更多相关文章
- Spring Boot @Autowired 没法自动注入的问题
Application 启动类: @SpringBootApplication @EnableConfigurationProperties @ComponentScan(basePackages = ...
- spring boot 2.0 提示 No primary or default constructor found for interface Pageable 解决办法
在SpringBoot 2.0 以前,我们会配置以下类 @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter ...
- Spring boot @Autowired注解在非Controller中注入为null
参考链接:https://blog.csdn.net/qq_35056292/article/details/78430777
- spring boot activiti 整合
1.pom.xml <dependency> <groupId>org.activiti</groupId> <artifactId>activiti- ...
- [Spring Boot] @Component, @AutoWired and @Primary
Spring boot is really good for Dependencies injection by using Autowiring. Each class instancse in s ...
- Spring Boot + Netty 中 @Autowired, @Value 为空解决
问题描述 使用 Spring Boot + Netty 新建项目时 Handler 中的 @Autowired, @Value 注解的始终为空值 解决方法 @Component // 1. 添加 @C ...
- spring boot 如何将没有注解的类@Autowired
等于将类交给spring管理,也就是IOC. 注解@Autowired是自动装配,也就是spring帮你创建对象,当然前提是这个@Autowired的类已经配置成Bean了,spring配置bean文 ...
- Spring/Spring boot正确集成Quartz及解决@Autowired失效问题
周五检查以前Spring boot集成Quartz项目的时候,发现配置错误,因此通过阅读源码的方式,探索Spring正确集成Quartz的方式. 问题发现 检查去年的项目代码,发现关于QuartzJo ...
- Spring Boot 自定义 Shiro 过滤器,无法使用 @Autowired 解决方法
在 Spring Boot 中集成 Shiro,并使用 JWT 进行接口认证. 为了统一对 Token 进行过滤,所以自定义了一个 JwtTokenFilter 过滤器. 期间遇到了以下几个问题,这里 ...
随机推荐
- [Go] 单元测试/性能测试 (go test)
特征 Golang 单元测试对文件名和方法名,参数都有很严格的要求.例如: 1.文件名必须以 _test.go 结尾 2.方法名必须是 Test 开头 3.方法参数必须是 t *testing.T 或 ...
- I/O会一直占用CPU吗?【转载】
转自:https://www.zhihu.com/question/27734728 知乎上看到的一个提问,可以参考 如下图:(图片摘自网络) 在进行I/O操作的时候,是将任务交给DMA来处理,请求发 ...
- spring mvc改动配置文件路径
1.1. Classpath project文件夹 在web.xml文件例如以下配置: <!-- 配置spring mvc 的核心servlet --> <servlet> ...
- Linux下怎么确定Nginx安装目录
linux环境下,怎么确定nginx是以那个config文件启动的? 输入命令行: ps -ef | grep nginx 摁回车,将出现如下图片: master process 后面的就是 ngi ...
- 【转】从源码来分析ListView
原文:http://yanmingming.sinaapp.com/?p=1251 原文其实不叫这个名字,本文对于原文有一定的修改,觉得这个名字比较适合本篇. 一.ListView 的工作原理 Ada ...
- crc16.c
static unsigned char auchCRCHi[];static unsigned char auchCRCLo[]; /* CRC 高位字节值表 */static unsigned c ...
- LaTeX技巧22:LaTeX文档中的参考文献初级
用 LaTeX 处理文档, 经常就要书写参考文献, 本篇就是介绍如何在 LaTeX 中使用参考文献, 注意这里讲的是LaTeX默认的 thebibliography 环境, 如果要了解 LaTeX 中 ...
- android studio运行时报错AVD Nexus_5X_API_P is already running解决办法
运行刚搭建好的Android环境时会报这种错误: AVD Nexus_5X_API_P is already running. If that is not the case, delete the ...
- mysql权限管理命令示例
mysql权限管理命令示例 grant all privileges on *.* to *.* identified by 'hwalk1'; flush privileges; insert in ...
- ASP.NET Core开发-读取配置文件Configuration appsettings.json
https://www.cnblogs.com/linezero/p/Configuration.html ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配 ...