0
点赞
收藏
分享

微信扫一扫

让代码更简洁 和@Autowired说分手, 迎接 @RequiredArgsConstructor注解

yongxinz 2022-09-05 阅读 52


在我们写controller或者Service层的时候,需要注入很多的mapper接口或者另外的service接口,这时候就会写很多的@AutoWired注解,代码看起来很乱
lombok提供了一个注解:

@RequiredArgsConstructor(onConstructor =@_(@Autowired))
写在类上可以代替@AutoWired注解,需要注意的是在注入时需要用final定义

@RestController
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class PushController {
private final XXXService xxxService;

@GetMapping("/demo")
public String push() {
return xxxService.push();
}
}


举报

相关推荐

0 条评论