I'm working on a spring project from our customer.
Below is the code for controller
@Log4j2
@RestController
@AllArgsConstructor
@RequestMapping(path = "/api/theapi")
@Api(value = "Description for the API")
public class TheAPIController {
private final ModelMapper modelMapper;
private final ObjectMapper objectMapper;
private final TheDemoService demoService;
...other code for controller
}
Below is the code for Service:
@Service
public class TheDemoService{ ... }
I was so surprise about 2 things:
Question 1: Why we need to use @AllArgsConstructor from project Lombok?
As per my understanding, Spring provide @RestController that Spring runtime container will initialize an Instance for our Controller. So that, having a constructor for our Controller seems like an invalid approach for using Spring Inversion of Control, is this correct?
Question 2. Because of using @AllArgsConstructor, somehow, the instance for demoService is to be injected
But again, I surprise because the code of Controller does not have @Autowired in combine with demoService.
In the actual code, there is no @Autowired for "private final TheDemoService demoService".
Hence, I could think of a possibility there, is that because of Lombok's @AllArgsConstructor would inject an instance of our TheDemoService via a constructor of
TheAPIController, I could not reason anything about this logic.