Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 358 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where is the @Autowired annotation supposed to go - on the property or the method?

#1
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?
Reply

#2
The annotation goes with the property, because that's what's being autowired; the property to be automatically set. [This tutorial][1] has a nice example. [This more advanced example][2] shows how to use qualifiers to disambiguate the wiring.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#3
According to [the Javadoc for Autowired][1], the annotation can be used on "a constructor, field, setter method or config method". See [the full documentation][2] 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.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through