当前位置:网站首页>[Lombok quick start]

[Lombok quick start]

2022-04-23 06:57:00 One duck, one duck

One 、 install Lombok

idea Installing a plug-in
 Insert picture description here

Two 、 Join the rely on

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.0</version>
    <scope>provided</scope>
</dependency>

 Insert picture description here

3、 ... and 、 annotation

1.@Getter/@Setter

Automatically generate getter/setter
 Insert picture description here

2.@ToString

Automatic override toString() Method , Will print out all variables
 Insert picture description here

3.@EqualsAndHashCode

Automatic generation equals(Object other) and hashcode() Method , Include all non static variables and non transient The variable of
 Insert picture description here
If some variables don't want to be added to the judgment , You can go through exclude exclude , You can also use of Specify some fields
 Insert picture description here

  • Q : Why is there only one whole @EqualsAndHashCode annotation , Instead of two separate @Equals and @HashCode?
  • A : stay Java There are provisions in , When two objects equals when , their hashcode It has to be the same , conversely , When hashcode Phase at the same time , The object is not necessarily equals. therefore equals and hashcode To be realized together , So as not to violate Java Specified circumstances occur

4.@NoArgsConstructor, @AllArgsConstructor, @RequiredArgsConstructor

These three are very similar , Are automatically generating the constructor of this class , The only difference is that the parameters of the generated constructor are different

  • @NoArgsConstructor : Generate a constructor without parameters
     Insert picture description here
  • @AllArgsConstructor : Generate a constructor with all parameters
     Insert picture description here
    Pay attention to one here Java Small pit of , When we don't specify a constructor ,Java The compiler will help us automatically generate a constructor without any parameters for this class , however If we write the constructor ourselves ,Java It won't automatically help us fill in the parameterless constructor

However, many places ( Like Spring Data JPA), It will be required that each class must have a parameterless constructor , So you're adding @AllArgsConstructor when , Make sure you make it up @NoArgsConstrcutor, Otherwise there will be all kinds of pits waiting for you

  • @RequiredArgsConstructor : Generate a include “ Specific parameters ” Constructor , Specific parameters refer to those that have plus final Variables of modifiers

 Insert picture description here
Add up , If all variables are normal , It's no use final Embellished words , Then a constructor without parameters will be generated

5. @Data

Integrated package , Just add @Data This annotation , It is equivalent to adding the following notes at the same time

  • @Getter/@Setter
  • @ToString
  • @EqualsAndHashCode
  • @RequiredArgsConstructor

 Insert picture description here

6. @Value

It's also an integration package , But he will set all the variables to final Of , The others are just like @Data equally , It is equivalent to adding the following notes at the same time

  • @Getter ( Note that there is no setter)
  • @ToString
  • @EqualsAndHashCode
  • @RequiredArgsConstructor

 Insert picture description here
The upper one @Data Suitable for use in POJO or DTO On , And this @Value annotation , It is suitable for adding to classes whose values do not want to be changed , Like the value of a class Once created, you don't want to be changed , I just hope we read it , Just add @Value annotation , That is to say @Value for immutable class Another thing to note , this lombok Annotations @Value And another one. Spring Annotations @Value Name bumping , stay import When not import In the wrong

7. @Builder

Automatically generate streaming set Value writing , I don't have to write about it any more setter 了
 Insert picture description here
Be careful , Although just add @Builder annotation , We can quickly set the value of the object by using the streaming method , however setter I still have to write something that can't be omitted , because Spring Or other frameworks will use objects in many places getter/setter To them / assignment So it's usually @Data and @Builder Will be used together on the same class , It is convenient for us to stream code , It's also convenient to do things

8. @Slf4j

Automatically generate the class log static const , If you want to type a journal, you can type it directly , No more manual new log Static constants

 Insert picture description here
except @Slf4j outside ,lombok It also provides variant annotations of other logging frameworks, which can be used , Like @Log、@Log4j… etc. , They all help us create a static Constant log, Just use different libraries

SpringBoot The default support is slf4j + logback The log framework of , So there's no need to make more settings , It can be used directly in SpringBoot project On , log The most commonly used series of annotations is @Slf4j

版权声明
本文为[One duck, one duck]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230558023514.html