且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是什么在Spring框架@Inject和@Autowired之间的区别?哪一个是什么情况下使用?

更新时间:2023-09-17 10:23:52

这里假设你指的是javax.inject.Inject$c$c>注释。 @Inject 是Java CDI的一部分( Java EE 6中(JSR-299)引入上下文和依赖注入)标准,阅读更多信息。春天已经选择使用支持 @Inject 同义词用自己的 @Autowired 注释。

Assuming here you're referring to the javax.inject.Inject annotations. @Inject is part of the Java CDI (Contexts and Dependency Injection) standard introduced in Java EE 6 (JSR-299), read more. Spring has chosen to support using @Inject synonymously with their own @Autowired annotation.

因此​​,要回答你的问题, @Autowired 是Spring自己的(传统)注释。 @Inject 是一个新的Java技术,名为CDI,定义了类似的春天依赖注入一个标准的组成部分。在Spring应用程序,这两个注释的工作原理春天已经决定支持除一些JSR-299注解自己以同样的方式。

So, to answer your question, @Autowired is Spring's own (legacy) annotation. @Inject is part of a new Java technology called CDI that defines a standard for dependency injection similar to Spring. In a Spring application, the two annotations works the same way as Spring has decided to support some JSR-299 annotations in addition to their own.