当前位置:首页 > 科技  > 软件

终于有人把Autowired注解讲清楚了,赞!!!

来源: 责编: 时间:2024-04-02 17:21:37 273观看
导读@Autowired是什么@Autowired 注解由 Spring 的 org.springframework.beans.factory.annotation.Autowired 类定义, 直译过来就是自动注入的意思。@Autowired的定义如下:@Target({ElementType.CONSTRUCTOR, ElementType

@Autowired是什么

@Autowired 注解由 Spring 的 org.springframework.beans.factory.annotation.Autowired 类定义, 直译过来就是自动注入的意思。HrJ28资讯网——每日最新资讯28at.com

@Autowired的定义如下:HrJ28资讯网——每日最新资讯28at.com

@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Autowired {    boolean required() default true;}

@Autowired 的使用场景

1.字段注入

将 @Autowired 直接应用于类的成员变量上。Spring 容器会自动为这些变量找到与其类型匹配的 Bean 实例,并进行注入。HrJ28资讯网——每日最新资讯28at.com

public class MyClass {    @Autowired    private MyService myService;}

2.构造器注入

将 @Autowired 应用于类的构造函数上。HrJ28资讯网——每日最新资讯28at.com

Spring 容器会自动解析构造函数的参数类型,并为这些参数找到与其类型匹配的 Bean 实例,然后注入到构造函数中。HrJ28资讯网——每日最新资讯28at.com

public class MyClass {    private MyService myService;        @Autowired    public MyClass(MyService myService) {        this.myService = myService;    }}

3.方法注入

将 @Autowired 应用于类的方法上。HrJ28资讯网——每日最新资讯28at.com

当类实例化时,Spring 容器会自动解析这些方法的参数类型,并为这些参数找到与其类型匹配的 Bean 实例,然后调用这些方法并注入参数。HrJ28资讯网——每日最新资讯28at.com

public class MyClass {    private MyService myService;    @Autowired    public void setMyService(MyService myService) {        this.myService = myService;    }}

需要注意的是,通过 @Autowired 注解实现依赖注入时,如果在 Spring 容器中找不到与某个依赖类型匹配的 Bean 实例(或者找到多个,但没有明确的优先级),那么 Spring 将抛出异常。HrJ28资讯网——每日最新资讯28at.com

除非将该注解的 required 属性设置为 false,这样在找不到匹配的 Bean 时,框架将不会抛出异常。HrJ28资讯网——每日最新资讯28at.com

public class MyClass {    @Autowired(required = false)    private MyService myService;}

@Autowired是如何工作的

在 Spring 中,AutowiredAnnotationBeanPostProcessor (AABP) 负责处理带有 @Autowired 注解的成员变量、Setter 方法。HrJ28资讯网——每日最新资讯28at.com

以下是 AABP 解析 @Autowired 的完整代码调用流程:HrJ28资讯网——每日最新资讯28at.com

当 Spring 容器实例化一个 Bean 时,会创建相应的 BeanDefinition 对象。BeanDefinition 包含了关于 Bean 的所有元数据信息。HrJ28资讯网——每日最新资讯28at.com

在容器实例化、配置和初始化 Bean 的过程中,它会调用 AABP 的 postProcessMergedBeanDefinition 方法,以收集与依赖注入相关的元数据。HrJ28资讯网——每日最新资讯28at.com

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 对象, 如果该对象尚不存在,会创建一个新的对象。HrJ28资讯网——每日最新资讯28at.com

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);}

上面的代码中,我在关键位置添加了注释,老铁们可以仔细看一下,上述代码的主要作用就是找到一个类中:HrJ28资讯网——每日最新资讯28at.com

  • 添加了@Autowired的属性信息,用 AutowiredFieldElement进行表示。
  • 添加了 @Autowired 的方法信息,用AutowiredMethodElement进行表示。

当依赖注入需要发生时,容器会调用 AABP 的 postProcessProperties 方法。HrJ28资讯网——每日最新资讯28at.com

该方法中会调用 InjectionMetadata 的 inject 方法来实际注入 @Autowired 注解的成员变量、成员方法:HrJ28资讯网——每日最新资讯28at.com

metadata.inject(bean, beanName, pvs);

最后,通过执行 AutowiredFieldElement 和 AutowiredMethodElement 的 inject 方法来实际注入属性值和方法参数。HrJ28资讯网——每日最新资讯28at.com

AutowiredFieldElement 的 inject 方法实现如下:HrJ28资讯网——每日最新资讯28at.com

@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 方法的实现如下:HrJ28资讯网——每日最新资讯28at.com

@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 注解的成员变量、方法。HrJ28资讯网——每日最新资讯28at.com

本文链接:http://www.28at.com/showinfo-26-80864-0.html终于有人把Autowired注解讲清楚了,赞!!!

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com

上一篇: 超火前端框架:公开叫板 Vue、React,拥有 5k star

下一篇: 可伸缩架构案例:数据太多,如何无限扩展你的数据库?

标签:
  • 热门焦点
  • Golang 中的 io 包详解:组合接口

    io.ReadWriter// ReadWriter is the interface that groups the basic Read and Write methods.type ReadWriter interface { Reader Writer}是对Reader和Writer接口的组合,
  • SpringBoot中使用Cache提升接口性能详解

    环境:springboot2.3.12.RELEASE + JSR107 + Ehcache + JPASpring 框架从 3.1 开始,对 Spring 应用程序提供了透明式添加缓存的支持。和事务支持一样,抽象缓存允许一致地使用各
  • 微信语音大揭秘:为什么禁止转发?

    大家好,我是你们的小米。今天,我要和大家聊一个有趣的话题:为什么微信语音不可以转发?这是一个我们经常在日常使用中遇到的问题,也是一个让很多人好奇的问题。让我们一起来揭开这
  • Python异步IO编程的进程/线程通信实现

    这篇文章再讲3种方式,同时讲4中进程间通信的方式一、 Python 中线程间通信的实现方式共享变量共享变量是多个线程可以共同访问的变量。在Python中,可以使用threading模块中的L
  • 一文搞定Java NIO,以及各种奇葩流

    大家好,我是哪吒。很多朋友问我,如何才能学好IO流,对各种流的概念,云里雾里的,不求甚解。用到的时候,现百度,功能虽然实现了,但是为什么用这个?不知道。更别说效率问题了~下次再遇到,
  • 新电商三兄弟,“抖快红”成团!

    来源:价值研究所作 者:Hernanderz 随着内容电商的概念兴起,抖音、快手、小红书组成的&ldquo;新电商三兄弟&rdquo;成为业内一股不可忽视的势力,给阿里、京东、拼多多带去了巨大压
  • OPPO、vivo、小米等国内厂商Q2在印度智能手机市场份额依旧高达55%

    7月20日消息,据外媒报道,研究机构的报告显示,在全球智能手机出货量同比仍在下滑的大背景下,印度这一有潜力的市场也未能幸免,出货量同比也有下滑,多家厂
  • 朋友圈可以修改可见范围了 苹果用户可率先体验

    近日,iOS用户迎来微信8.0.27正式版更新,除了可更换二维码背景外,还新增了多项实用功能。在新版微信中,朋友圈终于可以修改可见范围,简单来说就是已发布的朋友圈
  • 上海举办人工智能大会活动,建设人工智能新高地

    人工智能大会在上海浦江两岸隆重拉开帷幕,人工智能新技术、新产品、新应用、新理念集中亮相。8月30日晚,作为大会的特色活动之一的上海人工智能发展盛典人工
Top