可以看到此处依赖注入为空
控制层代码
@ApiOperation(value = "记录管理员的操作日志")
@GetMapping("/setOperationLog")
public void test(HttpServletRequest request,
Long id,
String name,
String unifiedSource) {
OperationLogService operationLogService=new OperationLogServiceImpl();
operationLogService.saveOperation(request,id,name,unifiedSource);
}
问题原因猜测
springboot项目在启动之后,由于Service层标明了@Service注解,Spring就会为其在容器中创建Bean对象,此时Spring会帮助其加载与装配,但由于我在控制层调用的Service层对象是由自己手动new的,并不是由Spring接管的代理对象,所以手动new的对象内部的依赖注入为空。
解决方案
在控制层使用Spring容器中的Service层对象,即在控制层中采用依赖注入的方式使用Service层对象,而不是自己手动创建,完成后的代码:
@Autowired
private OperationLogService operationLogService;
@ApiOperation(value = "记录管理员的操作日志")
@GetMapping("/setOperationLog")
public void test(HttpServletRequest request,
Long id,
String name,
String unifiedSource) {
operationLogService.saveOperation(request,id,name,unifiedSource);
问题解决,再次debug发现Service层依赖注入成功
算是低级错误了,猜想原因如若不对,还请大家指正,谢谢