Inject and Resource and Autowired annotations

后端 未结 3 568
南旧
南旧 2020-11-28 18:27

What\'s the difference between @Inject and @Resource and @Autowired annotations?

When should we use each of them?

3条回答
  •  自闭症患者
    2020-11-28 19:08

    |------------|---------------|---------------|---------------|-----------------------|
    |            | Setter/Field  | Constructor   | Applicable to | Matching order        |
    |            | injection     | injection     | type          |                       |
    |------------|---------------|---------------|---------------|-----------------------|
    | @Autowired |       X       |       X       |               | Type, Qualifier, Name |
    |------------|---------------|---------------|---------------|-----------------------|
    | @Inject    |       X       |       X       |               | Type, Qualifier, Name |
    |------------|---------------|---------------|---------------|-----------------------|
    | @Resource  |       X       |               |       X       | Name, Type, Qualifier |
    |------------|---------------|---------------|---------------|-----------------------|
    

    So in Spring dependency injection @Inject and @Autowired have exactly the same behaviour.

提交回复
热议问题