老铁,天天码的@Autowired你知道怎么实现的吗?

简介: 该文章主要介绍了Spring框架中@Autowired注解的工作原理,包括@Autowired注解的用途、实现机制以及源码分析。

前言

Autowired注解是spring提供自动装配功能的注解,通过这个注解,spring能够在bean的依赖注入时注入相应的bean属性。 作为Java开发者,每天都可能用到Autowire注解,理解这个注解的实现流程是很有必要的,本文将介绍@Autowired的工作原理。

spring通过一个AutowiredAnnotationBeanPostProcessor后置处理器来实现@Autowired的功能,这个后置处理器即实现了MergedBeanDefinitionPostProcessor的postProcessMergedBeanDefinition方法功能,又实现了InstantiationAwareBeanPostProcessorAdapter后置处理器的postProcessProperties方法和postProcessPropertyValues。

image.png

原理分析

下面我们先分析这两个后置处理的提供的功能。

一、Autowired注解元信息解析过程:MergedBeanDefinitionPostProcessor

public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor {
   
   

/**

* 提供修改beandefinition的回调函数

* @see AbstractAutowireCapableBeanFactory#applyMergedBeanDefinitionPostProcessors

*/

void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName);

/**

* 重置回调,可用于清理资源

* @see DefaultListableBeanFactory#resetBeanDefinition

*/

default void resetBeanDefinition(String beanName) {
   
   

}

}

既然MergedBeanDefinitionPostProcessor有修改beandefinition的能力,那么看下AutowiredAnnotationBeanPostProcessor是怎么运用这个能力的。


public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter

implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware {
   
   

//需要处理的注解类型

private final Set<Class<? extends Annotation>> autowiredAnnotationTypes = new LinkedHashSet<>(4);

//这里是注解和注解目标对应的对象类信息缓存,避免多次读取

private final Map<String, InjectionMetadata> injectionMetadataCache = new ConcurrentHashMap<>(256);

/**

* 这里可以看出默认会处理@Autowired,@Value,@Inject注解

*/

@SuppressWarnings("unchecked")

public AutowiredAnnotationBeanPostProcessor() {
   
   

this.autowiredAnnotationTypes.add(Autowired.class);

this.autowiredAnnotationTypes.add(Value.class);

this.autowiredAnnotationTypes.add((Class<? extends Annotation>)

ClassUtils.forName("javax.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader()));

}

/**
* 实现MergedBeanDefinitionPostProcessor接口方法,提供修改beandefinition的能力
*/
@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
   
   

//查找当前bean类型里所有加了autowiredAnnotationTypes注解的属性元信息

InjectionMetadata metadata = findAutowiringMetadata(beanName, beanType, null);

//将这些元信息和beandefinition绑定

metadata.checkConfigMembers(beanDefinition);

}

@Override

public void resetBeanDefinition(String beanName) {
   
   

this.lookupMethodsChecked.remove(beanName);

this.injectionMetadataCache.remove(beanName);

}

}

findAutowiringMetadata方法就是查找bean类型里符合autowiredAnnotationTypes的元信息


//根据class信息查找属性或方法的自动装配元信息

private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz, @Nullable PropertyValues pvs) {
   
   

// Fall back to class name as cache key, for backwards compatibility with custom callers.

String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());

// Quick check on the concurrent map first, with minimal locking.

//检查缓存中是否存储,避免多次查询,下次真正注入的时候也需要这个信息

InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);

if (InjectionMetadata.needsRefresh(metadata, clazz)) {
   
   

synchronized (this.injectionMetadataCache) {
   
   

metadata = this.injectionMetadataCache.get(cacheKey);

if (InjectionMetadata.needsRefresh(metadata, clazz)) {
   
   

if (metadata != null) {
   
   

metadata.clear(pvs);

}

metadata = buildAutowiringMetadata(clazz);

this.injectionMetadataCache.put(cacheKey, metadata);

}

}

}

return metadata;

}

//根据class信息查找自动装配注解元信息和目标属性和方法信息

private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
   
   

List<InjectionMetadata.InjectedElement> elements = new ArrayList<>();

Class<?> targetClass = clazz;

do {
   
   

//查找符合自动装配的属性注解元信息(属性对象)

final List<InjectionMetadata.InjectedElement> currElements = new ArrayList<>();

ReflectionUtils.doWithLocalFields(targetClass, field -> {
   
   

AnnotationAttributes ann = findAutowiredAnnotation(field);

if (ann != null) {
   
   

if (Modifier.isStatic(field.getModifiers())) {
   
   

if (logger.isInfoEnabled()) {
   
   

logger.info("Autowired annotation is not supported on static fields: " + field);

}

return;

}

boolean required = determineRequiredStatus(ann);

currElements.add(new AutowiredFieldElement(field, required));

}

});

//查找符合自动装配的方法注解元信息(方法对象)

ReflectionUtils.doWithLocalMethods(targetClass, method -> {
   
   

Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);

if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
   
   

return;

}

AnnotationAttributes ann = findAutowiredAnnotation(bridgedMethod);

if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
   
   

if (Modifier.isStatic(method.getModifiers())) {
   
   

if (logger.isInfoEnabled()) {
   
   

logger.info("Autowired annotation is not supported on static methods: " + method);

}

return;

}

if (method.getParameterCount() == 0) {
   
   

if (logger.isInfoEnabled()) {
   
   

logger.info("Autowired annotation should only be used on methods with parameters: " +

method);

}

}

boolean required = determineRequiredStatus(ann);

PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);

currElements.add(new AutowiredMethodElement(method, required, pd));

}

});

elements.addAll(0, currElements);

targetClass = targetClass.getSuperclass();

}

while (targetClass != null && targetClass != Object.class);

//返回所有符合条件的元信息(方法+属性对象)

return new InjectionMetadata(clazz, elements);

}

上面的步骤通过解析得到了符合自动装配注解bean的属性对象和方法对象,并和bean的definition进行绑定。这就是AutowiredAnnotationBeanPostProcessor利用MergedBeanDefinitionPostProcessor扩展点实现的功能。

二、Autowired注解注入对象流程:InstantiationAwareBeanPostProcessorAdapter

上一步这里只是解析到了需要自动装配的属性,还没有注入相应对象。那么注入对象的过程由AutowiredAnnotationBeanPostProcessor实现了InstantiationAwareBeanPostProcessorAdapter的功能。


public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter

implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware {
   
   

//处理属性的功能,即完整自动注入属性
@Override

public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
   
   

//查找自动装配元信息
InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs);

try {
   
   

//执行注入

metadata.inject(bean, beanName, pvs);

}

catch (BeanCreationException ex) {
   
   

throw ex;

}

catch (Throwable ex) {
   
   

throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex);

}

return pvs;

}

}
相关文章
|
Java 开发者 Spring
解析Spring中Bean的生命周期
解析Spring中Bean的生命周期
171 2
|
数据安全/隐私保护
基于SpringBoot+Vue+Java+Mysql 的简历招聘系统【源码】下
基于SpringBoot+Vue+Java+Mysql 的简历招聘系统【源码】
|
存储 监控 搜索推荐
深度解析:装机设置全攻略,打造个性化高性能电脑
装机不仅仅是一个技术活,更是一个充满乐趣和创造性的过程。通过精心的硬件选择、兼容性的考虑、操作系统的安装与优化、个性化的设置以及性能测试与调优,你可以打造出一台符合自己使用习惯、性能出众的个性化电脑。同时,不要忽视备份与恢复策略的重要性,确保你的数据和系统安全无忧。希望本文能够为你提供有用的指导和启发,让你在装机的道路上更加得心应手。
279 1
|
Kubernetes 负载均衡 安全
Cilium使用 (Cilium 3)
Cilium使用 (Cilium 3)
292 6
|
10月前
|
缓存 NoSQL Java
有Redis为什么还要本地缓存?谈谈你对本地缓存的理解?
有Redis为什么还要本地缓存?谈谈你对本地缓存的理解?
174 0
有Redis为什么还要本地缓存?谈谈你对本地缓存的理解?
|
11月前
|
机器学习/深度学习 数据挖掘 数据处理
深度学习之卫星图像中的环境监测
基于深度学习的卫星图像环境监测是指通过使用深度学习模型处理和分析来自卫星的遥感数据,以实现对地球环境的自动化监测和分析。这项技术极大提升了环境监测的效率、精度和规模,应用于气候变化研究、生态保护、自然灾害监测、城市扩张评估等多个领域。
472 1
|
程序员 编译器
详解 C 标准库 - <limits.h>
`&lt;limits.h&gt;` 是 C 标准库中的头文件,定义了各种基本数据类型的大小限制和特性,如 `CHAR_BIT`、`CHAR_MIN`、`CHAR_MAX` 等,涵盖了整数和字符类型的极限值。它提供了关键的宏常量,帮助程序员理解变量范围,确保代码的正确性和可移植性。
|
消息中间件 缓存 负载均衡
复盘女朋友面试4个月的RocketMQ面试题
这篇文章复盘了面试中关于RocketMQ的高频题目,包括架构组成、使用姿势、功能原理及高级特性,并强调了理解这些实现机制对于面试成功的重要性。
复盘女朋友面试4个月的RocketMQ面试题
|
数据采集 传感器 新能源
LabVIEW新能源汽车电池性能测试系统
LabVIEW新能源汽车电池性能测试系统
275 0
|
监控 测试技术 持续交付
设计通用灵活的LabVIEW自动测试系统
设计通用灵活的LabVIEW自动测试系统
313 0