java @Autowired @Resource @Inject 三个注解的区别
创始人
2024-05-28 19:30:42
0

javax.annotation.Resource

jdk 内置的,JSR-250 中的注解。

依赖注入通过 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor 来处理。

org.springframework.beans.factory.annotation.Autowired
org.springframework.beans.factory.annotation.Value

javax.inject.Inject

JSR-330 中的注解,作用同 @Autowired

依赖注入通过 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor 来处理。

org.springframework.beans.factory.annotation.Qualifier
javax.inject.Qualifier

依赖注入通过 org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver 来处理。

@Autowired

spring 自带的注解。

注入顺序

  1. 按照 type 在 上下文中查找匹配的 bean

  1. 如果有多个 bean,按照 name 进行匹配

  • 如果有 @Qualifier 注解,按照 @Qualifier 指定的 name 进行匹配

  • 如果没有,按照变量名进行匹配

  1. 匹配不到,报错。因为 required 默认为 true,不想注入设置此 bean @Autowired(required=false)。

@Inject

在 spring 中,@Inject 和 @Autowired 相同。

@Inject 和 @Autowired 区别

  • @Inject 是 javaee 6 及以上版本包里的。

  • @Autowired 可以设置 required=false 而 @Inject 没有这个属性。

@Resource

有两个重要的属性,name 和 type,spring 将 name 属性解析为 bean 的名字,type 解析为 bean 的类型。如果未指定 name,取变量名给 name 赋值。

CommonAnnotationBeanPostProcessor 中Resource 赋值源码

/*** Class representing injection information about an annotated field* or setter method, supporting the @Resource annotation.*/private class ResourceElement extends LookupElement {private final boolean lazyLookup;public ResourceElement(Member member, AnnotatedElement ae, @Nullable PropertyDescriptor pd) {super(member, pd);Resource resource = ae.getAnnotation(Resource.class);String resourceName = resource.name();Class resourceType = resource.type();this.isDefaultName = !StringUtils.hasLength(resourceName);if (this.isDefaultName) {resourceName = this.member.getName();if (this.member instanceof Method && resourceName.startsWith("set") && resourceName.length() > 3) {resourceName = Introspector.decapitalize(resourceName.substring(3));}}else if (embeddedValueResolver != null) {resourceName = embeddedValueResolver.resolveStringValue(resourceName);}if (Object.class != resourceType) {checkResourceType(resourceType);}else {// No resource type specified... check field/method.resourceType = getResourceType();}this.name = (resourceName != null ? resourceName : "");this.lookupType = resourceType;String lookupValue = resource.lookup();this.mappedName = (StringUtils.hasLength(lookupValue) ? lookupValue : resource.mappedName());Lazy lazy = ae.getAnnotation(Lazy.class);this.lazyLookup = (lazy != null && lazy.value());}@Overrideprotected Object getResourceToInject(Object target, @Nullable String requestingBeanName) {return (this.lazyLookup ? buildLazyResourceProxy(this, requestingBeanName) :getResource(this, requestingBeanName));}}

在变量名相同的情况下报错

The bean could not be injected as a because it is a JDK dynamic proxy that implements:

指定了不同type无法解决问题,跟进源码发现是 spring boot 把异常给处理了

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'productInit': 
Injection of resource dependencies failed; 
nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: 
Bean named 'example2ProductMapper' is expected to be of type 'com.alibaba.cloud.youxia.manager.ProductManager' but was actually of type 'com.sun.proxy.$Proxy47'

想到在 DefaultListableBeanFactory 中 beanDefinitionMap 按照名称和 BeanDefinition 键值对的问题,名称和注入的对象一一对应,不然就会出现不对应的问题

注入规则

  1. 如果未指定 name,取变量名从上下文中查找名称匹配的 bean 进行注入,找不到或者注入的变量名与类型无法对应抛出异常。

  1. 如果指定了 name,则从上下文中查找名称匹配的 bean 进行注入,找不到抛出异常。

  1. 如果指定了 type,有两种情况

  • 通过变量名从上下文中查找不到对应的 bean,则通过 type则从上下文中查找类型匹配的 bean 进行注入,找不到抛出异常。

  • 通过变量名从上下文中找到对应的 bean但是注入的类型与无法与DefaultListableBeanFactory 中 beanDefinitionMap中通过变量名得到的 BeanDefinition 类型一致,抛出异常。

  1. 既没有指定 name,又没有指定 type,默认按照变量名进行注入。

  1. 如果同时指定了 name 和 type,从上下文中找到唯一匹配的 bean 进行注入,找不到抛出异常。

匹配顺序为

变量名 → 指定的 name → 指定的 type

如果注入的 bean 变量名相同,但是类型不同,通过 name 指定是修改代码量最小的办法。

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...