1

I checked many tutorials but could not find a satisfactory answer.Does this annotation is performing Dependency injection of the properties.I have heard that this can be applied to a method,constructor or field but for what purpose.

user1281029
  • 1,513
  • 8
  • 22
  • 32
  • @Jack: This is not a duplicate, the focus of both questions differs a lot. – Ralph Aug 06 '13 at 06:20
  • I would recommend using the @Inject annotation going forward. – sunny Aug 06 '13 at 06:27
  • It's a very broad subject. Don't check tutorials to get the whole picture. Check the spring reference. More specifically the section on [`@Autowired`](http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-autowired-annotation) – Bart Aug 06 '13 at 06:33

2 Answers2

6

The @Autowired annotation is performing Dependency Injection.

If @Autowired is applied to

  • a field: then the dependency is stored in this field
  • a setter: then the setter is invoked, with the parameter that is determined by the same algorithm like for the field dependency injection
  • a constructor: then the constructor is invoked with the parameters determined by the same algorithm like for the field dependency injection  
Tarun
  • 3,456
  • 10
  • 48
  • 82
Ralph
  • 118,862
  • 56
  • 287
  • 383
0

We use @Autowired for :

a. Less abstraction in the system .

b. When Spring finds an @Autowired annotation used with setter methods, it tries to perform byType autowiring on the method.

Shuhail Kadavath
  • 448
  • 3
  • 13