0

I am beginner at Spring / Spring Boot and I have a look at some Spring / Spring Boot projects and realized that, in some of them DI is applied using constructor as shown below:

@RestController
@RequestMapping("/api/v1/core")
public class DemoController {

    private final SalaryService salaryService;
    private final EmployeeService employeeService;

    public DemoController(SalaryService salaryService, EmployeeService employeeService) {
        this.salaryService = salaryService;
        this.employeeService = employeeService;
    }
}

However, in some projects, DI is applied via annotations and the code is simplified. Is it possible to use annotations in Spring or Spring Boot and simplify the code by removing constructor? And if so, is that applied to Service, Repository and Controller?

  • The way you show it, there is no Autowired annotation, so how are you applying DI? You can also autowire the variables themselves, this doesn't have to be on the constructor – Stultuske May 25 '21 at 12:04
  • 1
    @Stultuske In a component (e.g. RestController), the Autowired annotation on the ctor is entirely optional. – Michael May 25 '21 at 12:08
  • @Stultuske Why do not post an answer instead of words? –  May 25 '21 at 12:17
  • @Michael You seems to be an experienced developer. Any explanation please? –  May 25 '21 at 12:17
  • 1
    @Maxwell an answer is also 'just words'. Secondly, I meant it as a comment, not an answer – Stultuske May 25 '21 at 12:18
  • @Stultuske Ok, no need these comments. –  May 25 '21 at 12:18
  • 1
    @Maxwell Well, think about it. You have already told Spring that it needs to instantiate the class by annotating the class with `@RestController`. Your class has precisely 1 constructor. Spring is clever enough to see that there is no ambiguity here; if it wants to instantiate the class, it will necessarily *have* to use that constructor. Adding `@Autowired` to the constructor doesn't help Spring, it's just extra noise. I believe you can use `@Autowired` on a constructor if you have 2 constructors and you want to instruct Spring on which one to choose, but I've never needed that functionality. – Michael May 25 '21 at 12:22
  • @Maxwell since my comment explains one way how you can remove the constructor and still autowire the values ... "no need" might be a wrong way to look at it – Stultuske May 25 '21 at 12:26
  • @Michael Thanks a lot. But being a newbee on Spring, I am really confused with `@Autowired` and `@ArgsConstructors` (no, required, etc.). From the training videos of Spring, I always think `@Autowired` is a vital part for DI. But I think it is wrong. Could you please clarify me by posting an answer by using the example in teh question with multiple args and using `@Autowired` and `@ArgsConstructors` (no, required, etc.)? –  May 25 '21 at 13:02

1 Answers1

0

You can use Lombok, by adding @RequiredArgsConstructor

@RestController
@RequestMapping("/api/v1/core")
@RequiredArgsConstructor
public class DemoController {

    private final SalaryService salaryService;
    private final EmployeeService employeeService;

}

Yes, it's applied to Service, Repository and Controller. You can also use @Autowired annotation on field or setter, but this way is not preferred. More details here: Spring @Autowire on Properties vs Constructor

befre
  • 79
  • 1
  • 7
  • 1
    Thanks amigo, voted up. But I need further explanations. –  May 25 '21 at 12:19
  • Do you mean that `@Autowired` is not preferred generally? Or for dependency injection. Any further explanations pls? –  May 25 '21 at 13:25