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

What are the benefits of Lombok?

2025-03-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail about the benefits of Lombok, the editor thinks it is very practical, so share it with you for reference. I hope you can get something after reading this article.

What are the benefits of Lombok?

Lombok is a very useful Java tool that can be used to help developers eliminate lengthy code in Java, especially for simple Java objects (POJO). It does this through annotations.

If you know more about Lombok, you can skip this paragraph first and look back directly. If you are not very familiar with it, you can simply understand it.

To use Lombok in a project, you need three steps:

Install the Lombok plug-in in IDE

At present, Lombok supports a variety of IDE, including mainstream Eclips, Intellji IDEA, Myeclipse and so on.

Install it in IDEA as follows:

Import related dependencies

Lombok supports the use of multiple build tools for import dependencies. Currently, it mainly supports maven, gardle, ant, etc.

The import method using maven is as follows:

Org.projectlombok lombok 1.18.12 provided

Third, use annotations in the code

Lombok simplifies code mainly through annotations, such as @ Data, @ Getter/@Setter, @ Builder, @ NonNull, and so on.

Using the @ Data annotation, you can simply define a Java Bean:

Import lombok.Data; @ Data public class Menu {private String shopId; private String skuMenuId; private String skuName;}

Using @ Data annotations on classes is equivalent to using @ ToString, @ EqualsAndHashCode, @ Getter, @ Setter, and @ RequiredArgsConstrutor annotations at the same time, which is very useful for POJO classes.

That is, automatically help to define toString, Getter, Setter and other methods in the Menu class in the example.

From the above example, you can see that we use the @ Data annotation to greatly reduce the amount of code and make the code very concise. This is also the main reason why many developers are keen to use Lombok.

In addition, different people have different views on the use of Lombok, because many people have used Lombok and have a good understanding of its advantages, so let's focus on what problems the use of Lombok will bring.

What are the disadvantages of Lombok?

Qiang X teammates

Because the use of Lombok requires developers to install the corresponding plug-ins in IDE.

If the plug-in is not installed, opening a Lombok-based project using IDE will prompt you with errors such as not finding a method. Causes the project to fail to compile.

That is, if one person on the project team uses Lombok, then others must also install the IDE plug-in. Otherwise, there will be no way of collaborative development.

More importantly, if Lombok is used in a jar package that we define, then all applications that depend on this jar package are required to install plug-ins, which is highly intrusive.

Code readability, low debuggability

Using Lombok in your code can really help reduce a lot of code, because Lombok helps generate a lot of code automatically.

However, this code is not generated until the compilation phase, so in the process of development, a lot of code is actually missing.

The extensive use of Lombok in the code will result in much less readability of the code, and it will also bring some problems to the code debugging.

For example, if we want to know which classes are referenced by the getter methods of a property in a class, it is not that simple.

There is a pit

Because Lombok makes code development very easy, it makes some developers over-dependent on it.

In the process of using Lombok, if you do not understand the underlying principles of various annotations, it is easy to produce unexpected results.

To take a simple example, we know that when we use @ Data to define a class, it automatically generates the equals () method for us.

However, if you only use @ Data instead of @ EqualsAndHashCode (callSuper=true), the default is @ EqualsAndHashCode (callSuper=false). In this case, the generated equals () method will only compare the properties of the subclass and will not consider the properties inherited from the parent class, regardless of whether the access to the parent property is open or not.

This may lead to unexpected results.

Impact upgrad

Because Lombok is very intrusive to the code, it may bring a big problem, that is, it will affect our upgrade to JDK.

According to the current upgrade frequency of JDK, a new version is released every six months, but Lombok as a third-party tool and maintained by the open source team, then its iterative speed is not guaranteed.

So, if we need to upgrade to a new version of JDK, it will be affected if the features are not supported in Lombok.

Another possible problem is that the upgrade of Lombok itself will be limited.

Because an application may rely on multiple jar packages, and each jar package may rely on a different version of Lombok, it is necessary to do version arbitration in the application, and we know that jar package version arbitration is not so easy, and the probability of problems is high.

Break the encapsulation

I think there are ways to avoid the above problems. But there is another important reason why some people reject the use of Lombok, that is, it will break the encapsulation.

As we all know, the three characteristics of Java include encapsulation, inheritance and polymorphism.

If we use Lombok directly in our code, it will automatically generate methods such as getter, setter, and so on, which means that all parameters in a class automatically provide setting and reading methods.

To take a simple example, let's define a shopping cart class:

@ Data public class ShoppingCart {/ / Commodity number private int itemsCount; / / Total Price private double totalPrice; / / Commodity details private List items = new ArrayList ();} / / examples come from "geek time-Beauty of Design patterns"

We know that the number of items, the details of goods and the total price in the shopping cart were actually related to the relationship before, and if they need to be modified, they should be modified together.

However, we used Lombok's @ Data annotation for the itemsCount and totalPrice attributes. Although we define them as private types, we provide getter and setter methods for public.

Externally, you can modify the values of these two properties at will through the setter method. We can call the setter method at will to reset the values of the itemsCount and totalPrice properties, which will also cause them to be inconsistent with the values of the items property.

The definition of object-oriented encapsulation is that internal data is hidden through access control, and the external can only access and modify internal data through the limited interface provided by the class. Therefore, exposing setter methods that should not be exposed is a clear violation of the object-oriented encapsulation feature.

It is a good practice not to provide getter/setter, but to provide only an addItem method for public, while modifying the itemsCount, totalPrice, and items properties.

This is the end of this article on "what are the benefits of Lombok?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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