从当前springboot的火热程度来看,java config的应用是越来越广泛了,在使用java config的过程当中,我们不可避免的会有各种各样的注解打交道,其中,我们使用最多的注解应该就是@Autowired注解了。本文就来聊聊Autowired的底层实现原理
一、Autowired注解的用法
1、概述
使用spring开发时,进行配置主要有两种方式,一是xml的方式,二是java config的方式。
spring技术自身也在不断的发展和改变,从当前springboot的火热程度来看,java config的应用是越来越广泛了,在使用java config的过程当中,我们不可避免的会有各种各样的注解打交道,其中,我们使用最多的注解应该就是@Autowired注解了。这个注解的功能就是为我们注入一个定义好的bean
2、应用
应用与构造方法注入
应用与setter方法注入
应用与属性注入
3、具体用法
@Autowired
替换:autowire属性,自动装配(按照类型装配,通过set方法,且方法可以省略)
位置:修饰属性,set方法
语法:@Autowired(required="true")
注意:
1.如果容器中没有一个可以与之匹配且required属性为true则会报异常NoSuchBeanDefinitionException
2.如果容器中有多个可以类型可以与之匹配,则自动切换为按照名称装配
3.如果容器中有多个可以类型可以与之匹配,则自动切换为按照名称装配,如果名称也没有匹配,则报异常NoUniqueBeanDefinitionException
二、Autowired自动装配的过程
首先要清楚java注解的核心其实时反射
1、在Spring源代码当中,Autowired注解位于包org.springframework.beans.factory.annotation之中,该包的内容如下:
核心代码如下
private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<>();
Class<?> targetClass = clazz;//需要处理的目标类
do {
final LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<>();
/*通过反射获取该类所有的字段,并遍历每一个字段,并通过方法findAutowiredAnnotation遍历每一个字段的所用注解,并如果用autowired修饰了,则返回auotowired相关属性*/
ReflectionUtils.doWithLocalFields(targetClass, field -> {
AnnotationAttributes ann = findAutowiredAnnotation(field);
if (ann != null) {//校验autowired注解是否用在了static方法上
if (Modifier.isStatic(field.getModifiers())) {
if (logger.isWarnEnabled()) {
logger.warn("Autowired annotation is not supported on static fields: " + field);
}
return;
}//判断是否指定了required
boolean required = determineRequiredStatus(ann);
currElements.add(new AutowiredFieldElement(field, required));
}
});
//和上面一样的逻辑,但是是通过反射处理类的method
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.isWarnEnabled()) {
logger.warn("Autowired annotation is not supported on static methods: " + method);
}
return;
}
if (method.getParameterCount() == 0) {
if (logger.isWarnEnabled()) {
logger.warn("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));
}
});
//用@Autowired修饰的注解可能不止一个,因此都加在currElements这个容器里面,一起处理
elements.addAll(0, currElements);
targetClass = targetClass.getSuperclass();
}
while (targetClass != null && targetClass != Object.class);
return new InjectionMetadata(clazz, elements);
}
总结:
Spring对@autowired注解的实现逻辑位于类:AutowiredAnnotationBeanPostProcessor(后置处理器)。@Autowied的本质就是new对象,因为spring的核心思想就是IOC,只是将控制权反转给了Spring框架,由它在底层通过注解或者配置文件帮我们new对象。
到此这篇关于Spring注解Autowired的底层实现原理详解的文章就介绍到这了,更多相关Spring注解Autowired内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
本文标题为:Spring注解Autowired的底层实现原理详解


基础教程推荐
- 一文了解Java 线程池的正确使用姿势 2023-06-17
- Project Reactor源码解析publishOn使用示例 2023-04-12
- 全局记录Feign的请求和响应日志方式 2023-01-09
- Java去掉小数点后面无效0的方案与建议 2023-02-18
- Java File类的概述及常用方法使用详解 2023-05-18
- 用java实现扫雷游戏 2022-12-06
- JVM分析之类加载机制详解 2023-04-06
- Java使用EasyExcel进行单元格合并的问题详解 2023-01-18
- 工厂方法在Spring框架中的运用 2023-06-23
- SpringBoot配置文件中密码属性加密的实现 2023-03-11