请教Controller跟Service里@AllArgsConstructor 注解作用

Blade 已结 2 3024
睁眼就困
睁眼就困 剑者 2019-11-25 11:41

请教Controller跟Service里@AllArgsConstructor 注解作用?

因为看到源码部分有这个注解部分没有这个注解.作用是什么?何时该用这个注解在Controller跟Service上?

image.png

=======更新20191128==========


我还描述的不够清楚看来.

我了解@AllArgsConstructor是lombok,生成全参构造函数用的.用于一般Entity/model对象上常用.

但是用于service/controller上,也为了生成全参构造函数?生成全参构造函数干啥用呢?

image.png

2条回答
  •  admin
    admin (最佳回答者)
    2019-11-25 20:12

    百度一下你就知道

    image.png

    作者追问:2019-11-25 20:12

    更新问题了.我知道这个注解,但是一般给实体对象用.从来没有给service/controller用,难道后面还有用到service跟controller的全参构造函数?

    1 讨论(3)
  • 2019-11-28 17:01

    常用的自动装配注解@Autowired @RequiredArgsConstructor @AllArgsConstructor

    https://blog.csdn.net/fwk19840301/article/details/91430905

    利用Lombok编写优雅的spring依赖注入代码,去掉繁人的@Autowired

    https://blog.csdn.net/Tybyqi/article/details/82872183


    Field injection is not recommended 依赖注入方式

    https://blog.csdn.net/github_38222176/article/details/79506392

    Constructor Injection in Spring with Lombok

    https://www.baeldung.com/spring-injection-lombok

    评论部分:

    Slava Semushin

    I don’t understand why the examples use @AllArgsConstructor while they actually should use @RequiredArgsConstructor. With the current approach, if you add another non-final member, code will be broken. However with @RequiredArgsConstructor it will continue to work.

    0 

    2 years ago


    Guest

    Grzegorz Piwowarek

    Well, it depends. I rarely use non-final fields and if I add one by mistake I’d love to be punished by a compiler and reminded that I did something wrong

    0 讨论(0)
提交回复