亲宝软件园·资讯

展开

spring注解@RequiredArgsConstructor

兴趣使然的小小 人气:0

Lombok在spring中的特殊注解@RequiredArgsConstructor

在了解lombok中的特殊注解之前:了解一下spring的常用注入方式戳这里

1.注解注入:

Controller
public class FooController {
  @Autowired
  //@Inject
  private FooService fooService;
  
  //简单的使用例子,下同
  public List<Foo> listFoo() {
      return fooService.list();
  }
}

2.构造器注入:

Controller
public class FooController {
  
  private final FooService fooService;
  
  @Autowired
  public FooController(FooService fooService) {
      this.fooService = fooService;
  }
  
  //使用方式上同,略
}

3.setter注入:

@Controller
public class FooController {
  
  private FooService fooService;
  
  //使用方式上同,略
  @Autowired
  public void setFooService(FooService fooService) {
      this.fooService = fooService;
  }
}

最后就是lombok中的@RequiredArgsConstructor

@RequiredArgsConstructor
public class VerifyController {

    private final VerifyService verifyService;
    private final InvitationService invitationService;
    private final VerificationCodeService verificationCodeService;

加载全部内容

相关教程
猜你喜欢
用户评论