前面文章Spring官方真的不加以使用属性注入吗,提到使用@Autowired进行依赖注入,那么你知道 @Autowired怎么工作的吗?
@Autowired是什么
@Autowired 注解由 Spring 的 org.springframework.beans.factory.annotation.Autowired 类定义,
直译过来就是自动注入的意思。
@Autowired的定义如下:
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
boolean required() default true;
}
@Autowired 的使用场景
1.字段注入
将 @Autowired 直接应用于类的成员变量上。Spring 容器会自动为这些变量找到与其类型匹配的 Bean 实例,并进行注入。
public class MyClass {
@Autowired
private MyService myService;
}
2.构造器注入
将 @Autowired
应用于类的构造函数上。
Spring
容器会自动解析构造函数的参数类型,并为这些参数找到与其类型匹配的 Bean
实例,然后注入到构造函数中。
public class MyClass {
private MyService myService;
@Autowired
public MyClass(MyService myService) {
this.myService = myService;
}
}
3.方法注入
将 @Autowired
应用于类的方法上。
当类实例化时,Spring
容器会自动解析这些方法的参数类型,并为这些参数找到与其类型匹配的 Bean
实例,然后调用这些方法并注入参数。
public class MyClass {
private MyService myService;
@Autowired
public void setMyService(MyService myService) {
this.myService = myService;
}
}
需要注意的是,通过 @Autowired
注解实现依赖注入时,如果在 Spring
容器中找不到与某个依赖类型匹配的 Bean
实例(或者找到多个,但没有明确的优先级),那么 Spring
将抛出异常。
除非将该注解的 required
属性设置为 false
,这样在找不到匹配的 Bean
时,框架将不会抛出异常。
public class MyClass {
@Autowired(required = false)
private MyService myService;
}
@Autowired是如何工作的
在 Spring 中,AutowiredAnnotationBeanPostProcessor (AABP)
负责处理带有 @Autowired
注解的成员变量、Setter
方法。
以下是 AABP 解析 @Autowired
的完整代码调用流程:
当 Spring
容器实例化一个 Bean
时,会创建相应的 BeanDefinition
对象。BeanDefinition
包含了关于 Bean
的所有元数据信息。
在容器实例化、配置和初始化 Bean
的过程中,它会调用 AABP
的 postProcessMergedBeanDefinition
方法,以收集与依赖注入相关的元数据。
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;
}
findAutowiringMetadata
方法会查找 Bean 的所有@Autowired
注解相关的元数据,并获取 InjectionMetadata
对象, 如果该对象尚不存在,会创建一个新的对象。
protected InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz, @Nullable PropertyValues pvs) {
// ... (省略无关代码)
List<InjectionMetadata.InjectedElement> elements = new ArrayList<>();
Class<?> targetClass = clazz;
// 遍历 Bean 的类结构,从子类向基类查找有@Autowired 注解的字段、方法和构造器
do {
final List<InjectionMetadata.InjectedElement> currElements = new ArrayList<>();
ReflectionUtils.doWithLocalFields(targetClass, field -> {
// 寻找带有@Autowired 注解的字段
MergedAnnotation<?> ann = findAutowiredAnnotation(field);
if (ann != null) {
if (Modifier.isStatic(field.getModifiers())) {
// 静态字段不能自动注入
// ... (省略错误处理和日志)
}
boolean required = determineRequiredStatus(ann);
// AutowiredFieldElement 属性Autowired元素
currElements.add(new AutowiredFieldElement(field, required));
}
});
ReflectionUtils.doWithLocalMethods(targetClass, method -> {
// 寻找带有@Autowired 注解的Setter方法或普通方法
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
return;
}
MergedAnnotation<?> ann = findAutowiredAnnotation(bridgedMethod);
if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
if (Modifier.isStatic(method.getModifiers())) {
// 静态方法不能自动注入
// ... (省略错误处理和日志)
}
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
boolean required = determineRequiredStatus(ann);
// AutowiredMethodElement 方法 Autowired 元素
currElements.add(new AutowiredMethodElement(method, required, pd));
}
});
elements.addAll(0, currElements);
targetClass = targetClass.getSuperclass();
}
while (targetClass != null && targetClass != Object.class);
// 构建并返回 InjectionMetadata 对象
return new InjectionMetadata(clazz, elements);
}
上面的代码中,我在关键位置添加了注释,老铁们可以仔细看一下,上述代码的主要作用就是找到一个类中:
-
添加了@Autowired的属性信息,用
AutowiredFieldElement
进行表示。 -
添加了 @Autowired 的方法信息,用
AutowiredMethodElement
进行表示。
当依赖注入需要发生时,容器会调用 AABP 的 postProcessProperties
方法。
该方法中会调用 InjectionMetadata
的 inject
方法来实际注入 @Autowired
注解的成员变量、成员方法:
metadata.inject(bean, beanName, pvs);
最后,通过执行 AutowiredFieldElement
和 AutowiredMethodElement
的 inject
方法来实际注入属性值和方法参数。
AutowiredFieldElement
的 inject
方法实现如下:
@Override
protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
Field field = (Field) this.member;
Object value;
if (this.cached) {
try {
value = resolvedCachedArgument(beanName, this.cachedFieldValue);
}
catch (NoSuchBeanDefinitionException ex) {
// Unexpected removal of target bean for cached argument -> re-resolve
value = resolveFieldValue(field, bean, beanName);
}
}
else {
value = resolveFieldValue(field, bean, beanName);
}
if (value != null) {
ReflectionUtils.makeAccessible(field);
field.set(bean, value);
}
}
AutowiredMethodElement
的 inject
方法的实现如下:
@Override
protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
if (checkPropertySkipping(pvs)) {
return;
}
Method method = (Method) this.member;
Object[] arguments;
if (this.cached) {
try {
arguments = resolveCachedArguments(beanName);
}
catch (NoSuchBeanDefinitionException ex) {
// Unexpected removal of target bean for cached argument -> re-resolve
arguments = resolveMethodArguments(method, bean, beanName);
}
}
else {
arguments = resolveMethodArguments(method, bean, beanName);
}
if (arguments != null) {
try {
ReflectionUtils.makeAccessible(method);
method.invoke(bean, arguments);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
}
通过以上流程,AutowiredAnnotationBeanPostProcessor
将解析并注入带有 @Autowired
注解的成员变量、方法。
今日分享如果对你有帮助,帮忙一键三连,感谢