Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Lombok to streamline your project code

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use Lombok to simplify your project code, the article is very detailed, has a certain reference value, interested friends must read it!

What is Lombok?

In a nutshell: lombok is a tool that provides simple annotations to simplify our repetitive and verbose Java code. For example, a Java Bean annotated @ Data of lombok:

@ Data public class NormalBean {private String name;}

Equivalent to the following code, the Getter/Setter,toString,equals and hashCode methods are automatically generated at compile time.

Public class NormalBean {private String name; public String getName () {return name;} public void setName (String name) {this.name = name;} @ Override public boolean equals (Object o) {if (this = = o) return true; if (o = = null | | getClass ()! = o.getClass () return false; NormalBean that = (NormalBean) o; return name! = null? Name.equals (that.name): that.name = = null;} @ Override public int hashCode () {return name! = null? Name.hashCode (): 0;} @ Override public String toString () {return "NormalBean {" + "name='" + name +'\'+'}';}}

There are more detailed documents on the official website of lombok.

Installation

Lombok should not only add jar packages to the project, but also turn on IDE support in the form of plug-ins. If your project is built by maven, you need to add the following dependencies to pom.xml:

Org.projectlombok lombok 1.16.10

It is recommended to manage pom dependencies in the form of bom. For example, lombok's dependencyManagement is included in Spring io platform.

Idea

It is easy to install lombok under idea. You can install lombok plugin in plugin.

Eclipse

Copy lombok.jar to the root of the folder where eclipse.ini is located

Edit the eclipse.ini and add at the end:

-Xbootclasspath/a:lombok.jar-javaagent:lombok.jar

Restart

For other IDE add lombok support, please see here.

Use

Lombok contains a lot of annotations, and if you use them all, you can really simplify a lot of code. But in real-world projects, we often use lombok only when defining Java Bean, but rarely in business code. Because lombok can simplify the code, but the disadvantages are also obvious:

Reduce the readability of the code

Debugging difficulty

In most cases, we should put the readability of the code at the top of the coding considerations. Here are some of the most commonly used lombok annotations I think:

@ Data

@ Setter/Getter

@ Log

@ NoArgsConstructor, @ RequiredArgsConstructor and @ AllArgsConstructor

@ Builder

@ Log and a series of related annotations (such as @ Log4j,@Slf4j) are my favorite annotations without having to write a bunch of getLogger. And @ Builder is also convenient enough to cope with a simple Builder schema.

The above is all the contents of the article "how to use Lombok to streamline your project code". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report