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

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

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

@Autowired是什么

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

@Autowired的定义如下:rBu28资讯网——每日最新资讯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 实例,并进行注入。rBu28资讯网——每日最新资讯28at.com

public class MyClass {    @Autowired    private MyService myService;}

2.构造器注入

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

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

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

3.方法注入

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

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

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

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

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

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

@Autowired是如何工作的

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

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

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

在容器实例化、配置和初始化 Bean 的过程中,它会调用 AABP 的 postProcessMergedBeanDefinition 方法,以收集与依赖注入相关的元数据。rBu28资讯网——每日最新资讯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 对象, 如果该对象尚不存在,会创建一个新的对象。rBu28资讯网——每日最新资讯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);}

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

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

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

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

metadata.inject(bean, beanName, pvs);

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

AutowiredFieldElement 的 inject 方法实现如下:rBu28资讯网——每日最新资讯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 方法的实现如下:rBu28资讯网——每日最新资讯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 注解的成员变量、方法。rBu28资讯网——每日最新资讯28at.com

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

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

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

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

标签:
  • 热门焦点
  • MIX Fold3包装盒泄露 新机本月登场

    小米的全新折叠屏旗舰MIX Fold3将于本月发布,近日该机的真机包装盒在网上泄露。从图上来看,新的MIX Fold3包装盒在外观设计方面延续了之前的方案,变化不大,这也是目前小米旗舰
  • 一加Ace2 Pro真机揭晓 钛空灰配色质感拉满

    终于,在经过了几波预热之后,一加Ace2 Pro的外观真机图在网上出现了。还是博主数码闲聊站曝光的,这次的外观设计还是延续了一加11的方案,只是细节上有了调整,例如新加入了钛空灰
  • 印度登月最关键一步!月船三号今晚进入环月轨道

    8月5日消息,据印度官方消息,月船三号将于北京时间今晚21时30分左右开始近月制动进入环月轨道。这是该探测器能够成功的最关键步骤之一,如果成功将开始围
  • Flowable工作流引擎的科普与实践

    一.引言当我们在日常工作和业务中需要进行各种审批流程时,可能会面临一系列技术和业务上的挑战。手动处理这些审批流程可能会导致开发成本的增加以及业务复杂度的上升。在这
  • 之家push系统迭代之路

    前言在这个信息爆炸的互联网时代,能够及时准确获取信息是当今社会要解决的关键问题之一。随着之家用户体量和内容规模的不断增大,传统的靠"主动拉"获取信息的方式已不能满足用
  • JavaScript学习 -AES加密算法

    引言在当今数字化时代,前端应用程序扮演着重要角色,用户的敏感数据经常在前端进行加密和解密操作。然而,这样的操作在网络传输和存储中可能会受到恶意攻击的威胁。为了确保数据
  • 拼多多APP上线本地生活入口,群雄逐鹿万亿市场

    Tech星球(微信ID:tech618)文 | 陈桥辉 Tech星球独家获悉,拼多多在其APP内上线了&ldquo;本地生活&rdquo;入口,位置较深,位于首页的&ldquo;充值中心&rdquo;内,目前主要售卖美食相关的
  • DRAM存储器10月价格下跌,NAND闪存本月价格与上月持平

    10月30日,据韩国媒体消息,自今年年初以来一直在上涨的 DRAM 存储器的交易价格仅在本月就下跌了近 10%,此次是全年首次降价,而NAND 闪存本月价格与上月持平。市
  • 最薄的14英寸游戏笔记本电脑 Alienware X14已可以购买

    2022年1月份在国际消费电子展(CES2022)上首次亮相的Alienware新品——Alienware X14现在已经可以购买了,这款笔记本电脑被誉为世界上最薄的 14 英寸游戏笔
Top