In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "IntelliJ IDEA how to use lombok to reduce lengthy code", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "IntelliJ IDEA how to use lombok to reduce lengthy code" bar!
For POJO, we need to generate getter,setter methods for each of these fields, although we can quickly generate them for us using IDE. However, if you need to change the field name and field type, then you need to delete and re-generate, after all, there is still some inconvenience. If we use lombok, we can generate the code we need directly through some simple annotations, which can greatly improve the development experience.
Pom introduces dependency
Org.projectlombok lombok true
Idea download plug-in
Introduction of common annotations in lombok
@ NonNull: if the field decorated with @ NonNull annotation is set to null through the set method, NullPointerException will be thrown
@ Cleanup: it is mainly used to modify IO stream related classes, and the resource is close () in the finally code block.
@ Getter,@Setter: generate a getter,setter method for the field, and mark it on the class to indicate that it is generated for all fields
@ ToString: generate the toString method, which prints all non-static fields by default
@ EqualsAndHashCode: generate equals and hashCode methods
@ NoArgsConstructor: no parameter constructor
RequiredArgsConstructor: generates a constructor for uninitialized final fields and fields annotated with @ NonNull
@ AllArgsConstructor: generate constructors for all fields
@ Data: equivalent to making @ Getter,@Setter,@ToString,@EqualsAndHashCode,@RequiredArgsConstructor at the same time
@ Value: the class will be decorated with final and @ ToString,@EqualsAndHashCode,@AllArgsConstructor,@Getter
Builder: create a static inner class that allows you to use chained calls to create objects such as name,age fields in the User object, User user=User.builder (). Name ("name"). Age (20). Build ()
@ SneakyThrows: throw an exception after try catch the annotated method. You can enter an exception array that requires catch in value. Default is catch Throwable.
@ Synchronized: wrap the code with synchronized ($lock) {} within the annotated method, and $lock is new Object [0]
@ Log,@CommonsLog,@JBossLog,@Log,@Log4j,@Log4j2,@Slf4j,@XSlf4j: generate a log object of the current class. You can use topic to specify the log name to be obtained, and use log... Use
Custom configuration
Although lombok can quickly generate code for us, some of the generated code still does not meet our needs. Lombok.config can be configured to solve the problem at this time
Some commonly used configurations are listed below
Lombok.getter.noIsPrefix=true (default: false) # lombok defaults to use the is prefix for get methods generated by fields of type boolean, and this configuration uses the get prefix lombok.accessors.chain=true (default: false) # default set method returns void set to true returns the object itself lombok.accessors.fluent=true (default: false) # if set to true. The get,set method will not take the get,set prefix, but directly use the field name as the method name lombok.log.fieldName=logger (default: log) # set the field name returned by the log class annotation
Note: in IDEA, please place the lombok.config file in the src\ main\ java directory. It will not take effect in src\ main\ resources.
Thank you for reading, the above is the content of "how IntelliJ IDEA uses lombok to reduce tedious code". After the study of this article, I believe you have a deeper understanding of how IntelliJ IDEA uses lombok to reduce tedious code, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.