The Spring @autowired annotation example shows you how to use the @Autowired annotation to inject a bean with constructor, field or setter method. Here the @Autowired annotation marks a constructor, field or setter method.Then Spring will look up exactly one bean that matches with the property is annotated and autowired automatically.

Create beans We are using these beans. The Job bean will be autowired in the Person bean. We are showing you three ways you can autowire, via setter, constructor and field injection.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

packagecom.javabycode.spring.core.autowired;

publicclassFruit{

privateStringname;

privateCountry country;

publicFruit(Country country){

this.country=country;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicvoidsetCountry(Country country){

this.country=country;

}

@Override

publicStringtoString(){

return"Fruit{"+

"name='"+name+'\''+

", country="+country+

'}';

}

}

Next, we create Country bean which is autowired into the Fruit bean.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

packagecom.javabycode.spring.core.autowired;

publicclassCountry{

privateStringname;

publicvoidsetName(Stringname){

this.name=name;

}

@Override

publicStringtoString(){

return"Country{"+

"name='"+name+'\''+

'}';

}

}

Enabling @Autowired Annotation in Spring configuration We enable the @Autowired annotation by adding element int the spring configuration file. This will register the AutowiredAnnotationBeanPostProcessor which takes care the processing of annotation.

Option 1: Setter Injection We can annotate a properties setter method by marking the @Autowired annotation.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

packagecom.javabycode.spring.core.autowired;

import org.springframework.beans.factory.annotation.Autowired;

publicclassFruit{

privateStringname;

privateCountry country;

publicvoidsetName(Stringname){

this.name=name;

}

@Autowired

publicvoidsetCountry(Country country){

this.country=country;

}

@Override

publicStringtoString(){

return"Fruit{"+

"name='"+name+'\''+

", country="+country+

'}';

}

}

Option 2: Constructor Injection

You can have only one constructor with the @Autowired annotation per class. This constructor does not have to be public. When there are multiple constructors with the annotation, a BeanCreationException will be thrown at startup.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

packagecom.javabycode.spring.core.autowired;

import org.springframework.beans.factory.annotation.Autowired;

publicclassFruit{

privateStringname;

privateCountry country;

@Autowired

publicFruit(Country country){

this.country=country;

}

publicvoidsetName(Stringname){

this.name=name;

}

@Override

publicStringtoString(){

return"Fruit{"+

"name='"+name+'\''+

", country="+country+

'}';

}

}

Option 3: Field Injection In Spring, fields are injected right after construction of a bean and set via reflection. This looks a lot simpler than constructor injection or setter injection

Conclusion: As you see above, the field injection looks more simpler than the construction injection and the setter injection. This is reason why I prefer the field injection than others. One more thing for the constructor injection, the constructors become very messy if the class has many dependencies.