Java - Spring - @Autowired @Resource @Inject (Wiring Types) - @Qualifier @Named
@Qualifier vs @Named
@Qualifier | @Named |
---|---|
belong to both Spring and Java extension package:
| belong to Java |
@Autowired vs @Resource vs @Inject
@Autowired | @Resource | @Inject |
---|---|---|
|
|
|
supports injecting:
| supports injecting:
| |
belongs to Spring
| belong to the Java extension package:
| belong to the Java extension package:
|
Which To Use
Scenario | @Resource | @Inject | @Autowired |
---|---|---|---|
Application-wide use of singletons through polymorphism | ✗ | ✔ | ✔ |
Fine-grained application behavior configuration through polymorphism | ✔ | ✗ | ✗ |
Dependency injection should be handled solely by the Jakarta EE platform | ✔ | ✔ | ✗ |
Dependency injection should be handled solely by the Spring Framework | ✗ | ✗ | ✔ |
Example Use
@Bean @Qualifier("appContext1") public com.context.AppContext appContext1() { return new AppContext(); } @Qualifier("appContext2") public com.context.AppContext appContext2() { return new AppContext(); }
given the configuration above, the following shows what's allowed and not:
// type case @Autowired AppContext appContext; // ERROR // qualifier case @Autowired @Qualifier("appContext1") AppContext appContext; // NO ERROR // named case @Autowired AppContext appContext1; // NO ERROR
, multiple selections available,