What is the difference between @Inject and @Autowired in Spring Framework? Under what conditions is it used?

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

The I AM going through some Blogs ON SpringSource and in One of at The Blogs, author IS a using @Injectand the I suppose of He CAN Also use @Autowired. I'm SpringSource on browse some blog in which a blog, the authors use is @Inject, I think he can Use @Autowired.

Here is the piece of code: This is a piece of code :

@Inject private CustomerOrderService customerOrderService;

At The AM not the Sure the About the I -difference the BETWEEN @Injectand @Autowiredand IT IF someone Explained Would Appreciate Their -difference and Which One to use an under the What Situation? I'm not sure @Inject, and @Autowiredthe difference between, if someone explains the difference between them, and under what circumstances Which one, I would appreciate it.


#1st Floor

Reference: https://stackoom.com/question/Ty7a/ What is the difference between -Inject and -Autowired in Spring-Framework-which one is used under what conditions


#2nd Floor

Spring 3.0 of AS, Spring Offers Support for JSR-330 Annotations dependency Injection ( @Inject, @Named, @Singleton). From the beginning Spring 3.0, Spring provides injection annotations (@ dependency on JSR-330 @Inject, @ @Named, @ @Singletonsupport) is.

IS A there separate sectionTop in Documentation The Spring About Them, Including Comparisons to Their Spring Equivalents. Spring document have about their individual parts , including Spring compared with their equivalents.


#3rd floor

@InjectNo "required" attribute


#4th floor

In addition to the above: In addition to the above :

  1. The default scope for @Autowiredbeans is Singleton whereas using JSR 330 @Injectannotation it is like Spring's prototype . @AutowiredThe default scope of the bean is Singleton, while using JSR 330 @Injectannotation is similar to Spring's prototype .
  2. There is no equivalent of @Lazy in JSR 330 using @Inject. In JSR 330, there is no @Inject@Lazy equivalent.
  3. IS NO equivalent of @Value there in the using JSR 330 @Inject. Used @Injectin JSR 330 No equivalent @Value.

#5th Floor

@Autowiredannotation is defined in the Spring framework. Annotation is defined in the Spring framework @Autowired.

@InjectAnnotation is a standard annotation, which is defined in the standard "Dependency Injection for Java" (JSR-330) . @InjectAn annotation is a standard annotation, defined in the standard "Java Dependency Injection " (JSR-330) . Spring (since the version 3.0) supports the generalized model of dependency injection which is defined in the standard JSR-330. Spring (since version 3.0) supports the general model of dependency injection defined in the standard JSR-330 . ( Google Guice frameworks and Picocontainer framework also support this model). ( Google Guice frameworks and Picocontainer framework also support this model).

With @InjectCAN BE Injected The Implementation of The Reference to The Providerinterface, Which android.permission Injecting References The deferreds. Use @Injectmay be injected to the Providerinterface reference, the interface allows the reference injection delay.

Annotations @Injectand @Autowired-is almost complete analogies. Annotations @Injectand @ @Autowiredalmost complete analogies. As well as @Autowiredannotation, @Injectannotation can be used for automatic binding properties, methods, and constructors. Like @Autowiredannotations, @ @Injectannotation can be used to automatically bind properties, methods, and constructors.

Contrast to the In @AutowiredAnnotation, @InjectAnnotation has NO requiredattribute. And @Autowiredannotation contrast, @ @Injectannotation no requiredattributes. Therefore, if the dependencies will not be found-will be thrown an exception. Therefore, if no dependencies are found, an exception will be thrown .

There are also differences in the clarifications of the binding properties . If there is ambiguity in the choice of components for the injection the @Namedqualifier should be added. If there is ambiguity in the choice of injected components, a @Named qualifier should be added . A Similar Situation for the In @AutowiredAnnotation Will BE added @Qualifierqualifier on (JSR-330 The Defines apos own IT @QualifierAnnotation and Annotation Via the this qualifier on @NamedIS defined). In similar circumstances, will @Autowiredadd comments @Qualifierqualifiers (JSR-330 defines its own @Qualifierannotations, And defined by this qualifier annotation @Named).


#6th floor

@AutowiredAnd @Injectthe main difference between the (reading Spring Docs noticed when), the @ @Autowiredhas a 'required' attribute, but @Inject not 'required' attribute.

Published 0 original articles · praised 8 · 30,000+ views

Guess you like

Origin blog.csdn.net/asdfgh0077/article/details/105538193