Reputation: 33
Could someone tell me what is the difference between
@Autowired
private MovieFinder movieFinder;
and
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
Upvotes: 1
Views: 92
Reputation: 4380
In the example you provided they are equivalent. The setter syntax allows you do other things, since you have method that gets called. For example:
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdcTemplate= new JdbcTemplate(dataSource);
}
I normally use the first type of notation, unless I have a special case like my example.
EDIT: Just to clarify, the first case is called field injection, and is done by setting the field directly using reflection. The second case is setter injection, and is done by calling the setter method. There's a third way of doing the injection, which is constructor injection.
Upvotes: 2