HiveBrain v1.2.0
Get Started
← Back to all entries
gotchajavaspringCritical

What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
injectconditionframeworkandunderbetweenusethedifferenceautowired

Problem

I am going through some blogs on SpringSource and in one of the blogs, the author is using @Inject and I suppose he can also use @Autowired.

Here is the piece of code:

@Inject private CustomerOrderService customerOrderService;

So what is the difference between using @Inject and @Autowired and would appreciate it if someone explained their difference and which one to use under what situation.

Solution

Assuming here you're referring to the javax.inject.Inject annotation, now called jakarta.inject.Inject. @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 the @Inject annotation synonymously with their own @Autowired annotation.

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

Context

Stack Overflow Q#7142622, score: 855

Revisions (0)

No revisions yet.