I have an Service Class and a Controller Class:
@Service
public class StudentService {
public List<Student> getStudents(){
return List.of(new Student(1L,
"Vitor",
"email@gmail.com",
LocalDate.of(1993, Month.JANUARY, 6),
28));
}
}
On the controller what is the difference (practical and/or theoretical) between the 2 implementations below? (Both works well on my tests)
1: @Autowired direct on the Service object.
@RestController
@RequestMapping(path = "api/v1/student")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping
public List<Student> getStudents(){
return studentService.getStudents();
}
}
2: @Autowired on the constructor
@RestController
@RequestMapping(path = "api/v1/student")
public class StudentController {
private final StudentService studentService;
@Autowired
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
@GetMapping
public List<Student> getStudents(){
return studentService.getStudents();
}
}
Thanks.