@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {
@Autowired
private CategoryService categoryService;
}
当我们将鼠标停留在@Autowired注解上时,会有提示显示Field injection is not recommended,也就是说这样的注解是不被springboot官方推荐的,具体原因可以看下面这篇博客
Field injection is not recommended(Spring团队不推荐使用Field注入)-CSDN博客
如果想解决这一问题,我们可以使用另一种代码
我们将private改为private final,再在整个项目上添加@RequiredArgsConstructor注解,如下面代码所示
@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
@RequiredArgsConstructor
public class CategoryController {
private final CategoryService categoryService;
}
这样警告就消失了,代码也会更加规范