Difference between @Inject and @Autowired

Difference between @Inject and @Autowired

This is one of the important interview question of spring / spring boot framework interviews. Auto-wiring is the great feature we got to wire our application dependencies spring way, but it can be done by both JSR annotation and spring provided annotation. It is important to know the difference between these both to make use of it effectively in real time use-cases.

@Inject:

  • Does not have ‘required’ attribute
  • Throws exception if the dependency not found.
  • Java/Sun Provided annotation (JSR 330)
  • Default bean scope is prototype.

 

@Autowired:

  • Has ‘required’ attribute
  • Exception can be avoided when no dependency found by setting required attribute to false.
  • Spring provided annotation.
  • Default bean scope is singleton.

235 total views, 5 views today

Leave a Reply

Your email address will not be published. Required fields are marked *