Spring中的注解@Autowired实现过程全解(@Autowired 背后的故事)
作者:IT老哥 时间:2023-01-01 06:02:53
现在面试,基本上都是面试造火箭🚀,工作拧螺丝🔩。而且是喜欢问一些 Spring 相关的知识点,比如 @Autowired 和 @Resource 之间的区别。魔高一丈,道高一尺。很快不少程序员学会了背诵面试题,那我反过来问“Spring 中的注解 @Autowired是如何实现的?”,“说说 @Autowired 的实现原理?”等等,背诵面试题的就露馅了。基于此,今天我们来说一说 @Autowired 背后的故事!
前言
使用 Spring 开发时,进行配置主要有两种方式,一是 xml 的方式,二是 Java config 的方式。Spring 技术自身也在不断的发展和改变,从当前 Springboot 的火热程度来看,Java config 的应用是越来越广泛了,在使用 Java config 的过程当中,我们不可避免的会有各种各样的注解打交道,其中,我们使用最多的注解应该就是 @Autowired 注解了。这个注解的功能就是为我们注入一个定义好的 bean。那么,这个注解除了我们常用的属性注入方式之外还有哪些使用方式呢?它在代码层面又是怎么实现的呢?这是本篇文章着重想讨论的问题。
@Autowired 注解用法
在分析这个注解的实现原理之前,我们不妨先来回顾一下 @Autowired 注解的用法。
将 @Autowired 注解应用于构造函数,如以下示例所示
`public class MovieRecommender {`
`private final CustomerPreferenceDao customerPreferenceDao;`
`@Autowired`
`public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {`
`this.customerPreferenceDao = customerPreferenceDao;`
`}`
`// ...`
`}`
将 @Autowired 注释应用于 setter 方法
`public class SimpleMovieLister {`
`private MovieFinder movieFinder;`
`@Autowired`
`public void setMovieFinder(MovieFinder movieFinder) {`
`this.movieFinder = movieFinder;`
`}`
`// ...`
`}`
将 @Autowired 注释应用于具有任意名称和多个参数的方法
`public class MovieRecommender {`
`private MovieCatalog movieCatalog;`
`private CustomerPreferenceDao customerPreferenceDao;`
`@Autowired`
`public void prepare(MovieCatalog movieCatalog,`
`CustomerPreferenceDao customerPreferenceDao) {`
`this.movieCatalog = movieCatalog;`
`this.customerPreferenceDao = customerPreferenceDao;`
`}`
`// ...`
`}`
您也可以将 @Autowired 应用于字段,或者将其与构造函数混合,如以下示例所示
`public class MovieRecommender {`
`private final CustomerPreferenceDao customerPreferenceDao;`
`@Autowired`
`private MovieCatalog movieCatalog;`
`@Autowired`
`public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {`
`this.customerPreferenceDao = customerPreferenceDao;`
`}`
`// ...`
`}`
直接应用于字段是我们使用的最多的一种方式,但是使用构造方法注入从代码层面却是更加好的,具体原因我就不细说了,有不懂的可以留言区评论。除此之外,还有以下不太常见的几种方式。
将 @Autowired 注释添加到需要该类型数组的字段或方法,则 Spring 会从ApplicationContext 中搜寻符合指定类型的所有 bean,如以下示例所示:
`public class MovieRecommender {`
`@Autowired`
`private MovieCatalog[] movieCatalogs;`
`// ...`
`}`
数组可以,我们可以马上举一反三,那容器也可以吗,答案是肯定的,下面是 set 以及 map 的例子:
`public class MovieRecommender {`
`private Set<MovieCatalog> movieCatalogs;`
`private Map<String, MovieCatalog> movieCatalogs;`
`@Autowired`
`public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {`
`this.movieCatalogs = movieCatalogs;`
`}`
`@Autowired`
`public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {`
`this.movieCatalogs = movieCatalogs;`
`}`
`// ...`
`}`
来源:https://blog.csdn.net/hnjsjsac/article/details/118540683
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于Kotlin写界面时诸多控件的点击事件
![](https://img.aspxhome.com/file/2023/5/137585_0s.jpg)
Java BigDecimal案例详解
![](https://img.aspxhome.com/file/2023/9/63509_0s.png)
详解Java线程池如何统计线程空闲时间
浅谈Gradle 常用配置总结
![](https://img.aspxhome.com/file/2023/1/137211_0s.png)
详解Java设计模式之备忘录模式的使用
![](https://img.aspxhome.com/file/2023/7/58437_0s.jpg)
Java类成员访问权限控制知识总结
![](https://img.aspxhome.com/file/2023/5/96075_0s.png)
SpringCloud之微服务容错的实现
![](https://img.aspxhome.com/file/2023/5/60775_0s.png)
解析C#设计模式编程中外观模式Facade Pattern的应用
![](https://img.aspxhome.com/file/2023/9/67549_0s.jpg)
很详细的android序列化过程Parcelable
java实现随机验证码图片生成
![](https://img.aspxhome.com/file/2023/1/82301_0s.jpg)
maven profile自动切换环境参数的2种方法详解
![](https://img.aspxhome.com/file/2023/6/64506_0s.png)
Android 详解Studio引用Library与导入jar
![](https://img.aspxhome.com/file/2023/3/137233_0s.png)
JSON各种转换问题(json转List,json转对象等)
SpringBoot如何在普通类加载Spring容器
详解SpringMVC的url-pattern配置及原理剖析
![](https://img.aspxhome.com/file/2023/1/110901_0s.png)
Android文件下载进度条的实现代码
.NET程序员如何入门Spring Boot详解
![](https://img.aspxhome.com/file/2023/6/104066_0s.png)
Java单例模式、饥饿模式代码实例
C#实现智能AI五子棋游戏详解
![](https://img.aspxhome.com/file/2023/6/111146_0s.png)