请帮忙解释一下Lombok的@AllArgsConstructor和spring的@RestController的奇怪组合

Please help to explain a strange combination of Lombok's @AllArgsConstructor and spring's @RestController

我正在处理我们客户的 spring 项目。

下面是控制器的代码

@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
}

下面是服务代码:

@Service
public class TheDemoService{ ... }

我对两件事感到非常惊讶:

问题 1:为什么我们需要使用 Lombok 项目中的@AllArgsConstructor?

据我了解,Spring 提供@RestController,Spring 运行时容器将为我们的控制器初始化一个实例。因此,为我们的 Controller 使用构造函数似乎是使用 Spring 控制反转的无效方法,这是正确的吗?

问题2.因为使用了@AllArgsConstructor,不知何故,要注入demoService的实例

但是,令我惊讶的是,Controller 的代码中并没有@Autowired 与demoService 结合使用。

在实际代码中,"private final TheDemoService demoService"没有@Autowired。

因此,我想到了一种可能性,因为 Lombok 的 @AllArgsConstructor 会通过

的构造函数注入我们的 TheDemoService 实例

TheAPIController,我无法对这个逻辑进行任何推理。

  1. 这是无效的方法,不需要为 RestController 定义构造函数

  2. 服务

if a class, which is configured as a Spring bean, has only one constructor, the Autowired annotation can be omitted and Spring will use that constructor and inject all necessary dependencies.

总结@AllArgsConstructorcan/should被删除