Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a mistake about @Inject in spring framework reference documentation 3.2.2?

Tags:

spring

The documentation contains following paragraph.

"As with @Autowired, it is possible to use @Inject at the class-level, field-level, method-level and constructor-argument level."

If I have not a mistake, I know @Inject annotation can be used field-level, method-level and constructor-argument level, can not be used class-level.

Inject Annotation Source Code :

@Target({ METHOD, CONSTRUCTOR, FIELD })
@Retention(RUNTIME)
@Documented
public @interface Inject {}

Is it true?

like image 563
user1702733 Avatar asked Nov 04 '22 00:11

user1702733


1 Answers

For me, they are almost equivalent, @Inject is part of the CDI introduced since Java EE 6 and @Autowired part of the Spring framework.

The @Autowired interface looks to have the same target as @Inject:

@Target(value={CONSTRUCTOR,FIELD,METHOD})
@Retention(value=RUNTIME)
@Documented
public @interface Autowired

I think it is a small error in Spring documentation chapter 5.11.1 because I never seen any @Autowired annotation on a class level.

This link shows the difference and limitation of each approach.

like image 178
рüффп Avatar answered Nov 09 '22 21:11

рüффп