Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the @Autowired annotation supposed to go - on the property or the method?

Which is more correct?

This (with the @Autowired annotation on the method)?

@Controller public class MyController {     private MyDao myDao;      @Autowired     public MyController(MyDao myDao)     {         this.myDao = myDao;     } 

This (with the @Autowired annotation on the property)?

@Controller public class MyController {     @Autowired     private MyDao myDao;      public MyController(MyDao myDao)     {         this.myDao = myDao;     } 

Where is the @Autowired annotation supposed to go?

like image 806
Thom Wilkie Avatar asked Sep 19 '10 16:09

Thom Wilkie


People also ask

Can we use @autowired on methods?

@Autowired can be used only on "a constructor, field, setter method or config method".

Where can annotation @autowired be used?

The @Autowired annotation can be used to autowire bean on the setter method just like @Required annotation, constructor, a property or methods with arbitrary names and/or multiple arguments.

Which is the correct Autowiring in Spring?

Autowiring feature of spring framework enables you to inject the object dependency implicitly. It internally uses setter or constructor injection. Autowiring can't be used to inject primitive and string values. It works with reference only.

How does @autowired works internally?

Suppose I have a bean named HelloWorld which has a member attribute points to another bean User. With annotation @Autowired, as long as getBean is called in the runtime, the returned HelloWorld instance will automatically have user attribute injected with User instance.


1 Answers

According to the Javadoc for Autowired, the annotation can be used on "a constructor, field, setter method or config method". See the full documentation for more details.

I personally prefer your first option (constructor injection), because the myDao field can be marked as final:

@Controller public class MyControllear {     private final MyDao myDao;      @Autowired     public MyController(MyDao myDao) {       this.myDao = myDao;     } 

Constructor injection also allows you to test the class in a unit test without code that depends on Spring.

The second option would be better written as:

@Controller public class MyControllear {     @Autowired     private MyDao myDao;      MyController() {     } 

With field injection, Spring will create the object, then update the fields marked for injection.

One option you didn't mention was putting @Autowired on a setter method (setter injection):

@Controller public class MyControllear {     private MyDao myDao;      MyController() {     }      @Autowired     public void setMyDao(MyDao myDao) {       this.myDao = myDao;     } 

You do not have to choose one or another. You can use field injection for some dependencies and constructor injection for others for the same object.

like image 157
NamshubWriter Avatar answered Oct 01 '22 00:10

NamshubWriter