[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 过滤器. 期间遇到了以下几个问题,这里 ...
随机推荐
- VS2015 打包winform 安装程序
最近开发了一个小软件.由于需要打包.网上找了一些资料.然后整合了起来.希望对大家有所帮助.不全面请见谅. 打包控件 InstallShield-Limited-Edition 下面是注册地址 htt ...
- 咏南中间件支持TMS WEB CORE客户端
咏南中间件支持TMS WEB CORE客户端 TMS WEB CORE是优秀的JS前端,搭配咏南中间件后端,可以进行快速的企业应用开发.
- 怎样正确的使用Cookie的Path详细解析
原文地址:http://java-zone.org/1052.html cookie 有路径--path,表示哪些路径下的文件有权限读取该 cookie. path 应该以 “/” 结尾,同名 coo ...
- Xcode工程文件打不开:cannot be opened because the project file cannot be parsed
svn更新代码后,打开xcode工程文件,会出现 xxx..xcodeproj cannot be opened because the project file cannot be parsed ...
- 寂静之地百度云在线观看迅雷下载A Quiet Place高清BT下载
原名:A Quiet Place 地区:美国 语言:英语 / 美国手语 首播:2018-05-18(中国大陆) / 2018-03-09(西南偏南电影节) / 2018-04-06(美国) 电视台 ...
- centos升级到最新的mysql
去站点下载mysql的yum源.地址例如以下: http://repo.mysql.com/ 在linux上先查看系统的版本,依据版本相应下载 more /etc/redhat-release rpm ...
- Android避免过度绘制之道
如果一个布局十分复杂,那么就需要来排查是否出现了过度绘制,如果出现了,那么很可能会造成刷新率下降,造成卡顿的现象.那么什么是过度绘制呢?过度绘制就是在同一个区域中叠加了多个控件.这就像小时候我们画画, ...
- 删除list中的特定元素
对于动态删除list中的特定元素,一般用linkedList,删除时有以下两种方法. 1. 循环遍历,找到要删除的元素后删除并且减少list长度.如果不减少list长度,那么就仅仅删除了元素,但没改变 ...
- 【转载】理解Android中垃圾回收日志信息
本文转自:http://droidyue.com/blog/2014/11/08/understanding-garbage-collection-output-messages-in-android ...
- Eclipse中输入变量会自动补全上屏的解决方法
我自己在启动Eclipse代码补全后输入感觉确实爽多了,但是每次输入变量后一按下空格,编译器会自己帮你写一个很蛋疼的名字,比如你输入了:String mStr后按下空格,它就变成了mString,十分 ...