本篇内容主要讲解“spring中的特殊注解@RequiredArgsConstructor怎么用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“spring中的特殊注解@RequiredArgsConstructor怎么用”吧!
1.注解注入:
Controllerpublic class FooController { @Autowired //@Inject private FooService fooService; //简单的使用例子,下同 public List<Foo> listFoo() { return fooService.list(); }}
2.构造器注入:
Controllerpublic class FooController { private final FooService fooService; @Autowired public FooController(FooService fooService) { this.fooService = fooService; } //使用方式上同,略}
3.setter注入:
@Controllerpublic class FooController { private FooService fooService; //使用方式上同,略 @Autowired public void setFooService(FooService fooService) { this.fooService = fooService; }}
最后就是lombok中的@RequiredArgsConstructor
@RequiredArgsConstructorpublic class VerifyController { private final VerifyService verifyService; private final InvitationService invitationService; private final VerificationCodeService verificationCodeService;
到此,相信大家对“spring中的特殊注解@RequiredArgsConstructor怎么用”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!