本文翻译自:What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?

I am going through some blogs on SpringSource and in one of the blogs, author is using @Inject and I suppose he can also use @Autowired . 我正在SpringSource上浏览一些博客,在其中一个博客中,作者使用的是@Inject ,我想他也可以使用@Autowired

Here is the piece of code: 这是一段代码:

@Inject private CustomerOrderService customerOrderService;

I am not sure about the difference between @Inject and @Autowired and would appreciate it if someone explained their difference and which one to use under what situation? 我不确定@Inject@Autowired之间的区别,如果有人解释了它们的区别,以及在什么情况下使用哪个,我将不胜感激。


#1楼

参考:https://stackoom.com/question/Ty7a/在Spring-Framework中-Inject和-Autowired有什么区别-在什么条件下使用哪一个


#2楼

As of Spring 3.0, Spring offers support for JSR-330 dependency injection annotations ( @Inject , @Named , @Singleton ). 从Spring 3.0开始,Spring提供了对JSR-330依赖项注入注释(@ @Inject ,@ @Named ,@ @Singleton )的支持。

There is a separate section in the Spring documentation about them, including comparisons to their Spring equivalents. Spring文档中有一个关于它们的单独部分 ,包括与它们的Spring等效项的比较。


#3楼

@Inject没有“必需”属性


#4楼

In addition to the above: 除了上述内容:

  1. The default scope for @Autowired beans is Singleton whereas using JSR 330 @Inject annotation it is like Spring's prototype . @Autowired bean的默认作用域是Singleton,而使用JSR 330 @Inject注释则类似于Spring的prototype
  2. There is no equivalent of @Lazy in JSR 330 using @Inject . 在JSR 330中,没有使用@Inject的@Lazy等效项。
  3. There is no equivalent of @Value in JSR 330 using @Inject . 使用@Inject在JSR 330中没有等效的@Value。

#5楼

@Autowired annotation is defined in the Spring framework. @Autowired注释在Spring框架中定义。

@Inject annotation is a standard annotation, which is defined in the standard "Dependency Injection for Java" (JSR-330) . @Inject批注是一个标准批注,在标准“ Java依赖项注入”(JSR-330)中定义 。 Spring (since the version 3.0) supports the generalized model of dependency injection which is defined in the standard JSR-330. Spring(自版本3.0起)支持标准JSR-330中定义的依赖项注入的通用模型。 ( Google Guice frameworks and Picocontainer framework also support this model). ( Google Guice框架和Picocontainer框架也支持此模型)。

With @Inject can be injected the reference to the implementation of the Provider interface, which allows injecting the deferred references. 使用@Inject可以注入对Provider接口实现的引用,该接口允许注入延迟的引用。

Annotations @Inject and @Autowired - is almost complete analogies. 注释@Inject和@ @Autowired几乎是完整的类比。 As well as @Autowired annotation, @Inject annotation can be used for automatic binding properties, methods, and constructors. @Autowired注释一样,@ @Inject注释可用于自动绑定属性,方法和构造函数。

In contrast to @Autowired annotation, @Inject annotation has no required attribute. @Autowired注释相反,@ @Inject注释没有required属性。 Therefore, if the dependencies will not be found - will be thrown an exception. 因此,如果找不到依赖项,则将引发异常。

There are also differences in the clarifications of the binding properties. 结合特性的澄清也有所不同。 If there is ambiguity in the choice of components for the injection the @Named qualifier should be added. 如果在注入的成分选择上存在歧义,则应添加@Named限定词。 In a similar situation for @Autowired annotation will be added @Qualifier qualifier (JSR-330 defines it's own @Qualifier annotation and via this qualifier annotation @Named is defined). 在类似的情况下,将为@Autowired注释添加@Qualifier限定符(JSR-330定义了它自己的@Qualifier注释,并通过此限定符注释定义了@Named )。


#6楼

@Autowired@Inject之间的主要区别(在阅读Spring Docs时注意到)是,@ @Autowired具有'required'属性,而@Inject没有'required'属性。

在Spring Framework中@Inject和@Autowired有什么区别? 在什么条件下使用哪一个?相关推荐

  1. spring中@Inject和@Autowired的区别?分别在什么条件下使用呢?

    问题:spring中@Inject和@Autowired的区别?分别在什么条件下使用呢? 我在浏览SpringSource上的一些博客,在其他一个博客中,那个作者用了@Inject,但是我觉得他用@A ...

  2. Spring Framework 中文官方文档

    Spring Framework 中文官方文档 目前为最新版 5.3.11. 其他spring文档不断更新中 1.历史.设计理念.反馈.入门. Overview 2.IoC 容器.事件.资源.i18n ...

  3. Spring Framework 中文文档

    点击下方蓝色字体链接即可跳转 Spring Framework 中文文档

  4. spring配置中id和name属性的区别

    可能大家在网上都应该搜索过在 Spring 配置中 id 和 name 属性的区别,可能你会搜索到有一大堆的区别,不过在我这里可能不一样了. 我这里 Spring 的版本为 3.2.4,区别不是很大, ...

  5. Spring Framework中的作用域代理

    Spring框架提供了Spring bean的作用域. 我们可以使用spring范围控制插入Spring bean的各种依赖关系和配置值. Spring支持以下六个范围. 在基于Web的应用程序中可以 ...

  6. spring配置jndi_在Spring Framework中通过JNDI进行配置

    spring配置jndi 从某个时候开始,应用程序必须是可配置的. 从第一个版本0.9开始,Spring Framework就为该问题提供了一个很好的辅助工具,该类为PropertyPlacehold ...

  7. 在Spring Framework中通过JNDI进行配置

    从某个时候开始,应用程序必须是可配置的. 自第一个版本0.9起,Spring Framework就为该问题提供了一个很好的辅助工具,该类为PropertyPlaceholderConfigurer类, ...

  8. spring框架中JDK和CGLIB动态代理区别

    转载:https://blog.csdn.net/yhl_jxy/article/details/80635012 前言 JDK动态代理实现原理(jdk8):https://blog.csdn.net ...

  9. Spring Cloud中的Eureka和Zookeeper的区别在哪?

    首先为自己打个广告,我目前在某互联网公司做架构师,已经有5年经验,每天都会写架构师系列的文章,感兴趣的朋友可以关注我和我一起探讨,关注我,免费分享Java基础教程,以及进阶的高级Java架构师教程,全 ...

最新文章

  1. vscode 在ubuntu的terminal中下划线不显示解决方案
  2. java 添加注解_你知道Java中的package-info的作用吗?
  3. Css制作table细线表格
  4. java jsch session 多久会断开_What?纯Java居然能实现Xshell!
  5. GNN上用到的Tasks,Dataset and Benchmark
  6. DatePicker 和 DatePickerDialog的基本使用方法
  7. tab点击一个按钮切换排序_Axure8原型教程:实现Tab选项切换
  8. 抓包工具之MitmProxy
  9. 网络交换机怎么连接硬盘录像机、网络交换机怎么连接摄像头
  10. 集成运算放大器不负责任讲解1
  11. 计算机学院条幅内容,学院迎新活动标语横幅
  12. 函数最值题目及答案_关于函数的习题及答案
  13. 关于STM32平衡小车的几个关键疑问
  14. Thomson.Reuters.EndNote.X6.v16.0.0.6348.Cracked-EAT
  15. 英国《物理世界》杂志评选出世界十大物理学家
  16. 南邮 OJ 1275 登山机器人问题
  17. [Unity插件]Flux 插件
  18. (TS)根据身份证号码计算性别和年龄
  19. 如何优化selenium webdriver的执行速度
  20. 在Win10中使用YAMAHA S-YXG50软波表

热门文章

  1. webstrom Certificate validation failed
  2. Android 自定义控件开发入门(一)
  3. Java用Xom生成XML文档
  4. Android初学第29天
  5. HQL查询(分页查询,动态sql查询,参数查询)
  6. HTML页面中点击按钮关闭页面几种方式与取消
  7. 按键的使用(一)------verilog
  8. Android ImageView的ScaleType属性
  9. Apache Oltu 实现 OAuth2.0 服务端【授权码模式(Authorization Code)】
  10. ASP.NET中如何实现负载均衡