3

Since Spring 3.0, Spring supports the standard JSR 330: Dependency Injection for Java. In a Spring 3 application, you can use

  • @Inject instead of Spring’s @Autowired to inject a bean.
  • @Named instead of Spring’s @Component to declare a bean.

When should I use what?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Sawan Kumar
  • 75
  • 1
  • 9

3 Answers3

4

The @Inject annotation also serves the same purpose, but the main difference between them is that @Inject is a standard annotation for dependency injection and @Autowired is spring specific.

Read more: https://javarevisited.blogspot.com/2017/04/difference-between-autowired-and-inject-annotation-in-spring-framework.html#ixzz6Bwt3RUg9

Vüsal
  • 2,580
  • 1
  • 12
  • 31
Sarvesh
  • 41
  • 3
4

Long story short these annotations are spring guys efforts to support some parts of Java EE (now Jakarta EE) specification, these are injection official annotations there. Spring guys add them in order to facilitate Spring adoption and system migrations from Java EE.

So when to use this annotation?, use them if you are planning to migrate to/from Jakarta EE it makes sense to used them to facilitate the process or keep your system consistent. Otherwise, go for Spring "official" ones.

Juan Rada
  • 3,513
  • 1
  • 26
  • 26
1

If you only use spring, and do not special behaviour, you can use any annotations: they will be handled the same.

If you use (or plan to use) other dependency managers (JavaEE, Guice, XWiki internal container, ...) @Inject and @Named are defined by the JSR-330 and are now supported by all of those containers.

On the other side, if you plan to use all spring specific goodies, spring specific annotations may allow finer configuration.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252