As a senior back-end development, and why others never recommend the use of Lombok, talk about my views ...

Lombok is a very useful Java third-party tools that can help Java developers to eliminate lengthy codes, especially for simple Java objects (POJO).

Lombok has brought us convenience, but also brought no small risk.

Given that many pit himself stepped on, so I never take the initiative to recommend others to use Lombok, however, did not interfere with others. And some of my colleagues around me because the code is still used, so I was forced to install the plug-in Lombok.

And that's my attitude towards Lombok. Since this topic to talk about, we have to analyze the characteristics of Lombok under:

1, Lombok use

1.1 IDE plug-in installed Lombok

Lombok currently supports a variety of IDE, including mainstream Eclips, Intellji IDEA, Myeclipse are all supported.

Installation in IDEA as follows:

File → Settings → Plugins, enter "lombok", select "install" to install it, you need to restart after installation IDEA to run.

1.2 introducing its dependencies

Lombok supports the use of multiple tools to build import dependence, is currently the main support maven, gardle, ant etc support.

As used maven introduced as follows:

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

1.3 code using annotations

Lombok way to streamline the code is mainly achieved through annotations, which are commonly used @ Data, @ Getter / @ Setter, @ Builder, @ NonNull and so on.

Such as the use @Data annotations, you can simply define a Java Bean:

import lombok.Data;

@Data
public class User {
    private String id;
    private String age;
    private String name;
    private String job;
}

Use @Data annotation on the class, this way, this entity class properties do not need to get, set, and the toString method, which is useful for POJO class.

Meaning 1.4 annotations

  • @Date : added to the class, is equivalent to giving @ ToString, @ EqualsAndHashCode, @ Getter , @ Setter and @RequiredArgsConstrutor These annotations;
  • @AllArgsConstructor : added to the class, can be configured to generate full-containing entity class parameter;
  • @NoArgsConstructor : added to the class constructor with no arguments can be generated;
  • @RequiredArgsConstructor : added to the class, with @NonNull annotations using the specified parameters generated constructor. For example, in front of the property plus age @NonNull annotation, the User generates a parameter configuration method requires age;
  • @Getter : added to the class, the entity class may be generated getter methods all properties;
  • @Setter : added to the class, the entity class may be generated setter methods for all attributes;
  • @ToString : added to the class, calling toString () method, the output values of all attributes entity class.
     

2, Lombok What are the benefits?

By the above example, we can find, use @Data annotation greatly reduces the amount of code, the code is very simple, and this is the main reason many developers are keen to use in Lombok.

Not only that, I also cited other advantages Lombok:

  • Reducing the template code: .lombok processing get, set, toString, hash, equal or the like, a large amount of code templates package, reducing code duplication, when adding new properties when the above methods do not need to be rewritten;
  • Enhance code readability: focus on class property definitions do not need to go into publishing a waste of time;
  • Reduce code maintenance: new attributes when the code will reduce a lot of maintenance work.

3, Lombok any harm?

Since I never take the initiative to recommend others to use Lombok, it must be stepped rule of thumb many pit.

So, let's talk about the focus of Lombok in the course of which issues will bring.

3.1 forcing teammates

Use Lombok widget developers requires corresponding plug must be installed in the IDE. Not only we have to install, and your co-development of any person required to install.

If anyone plugin is not installed, use the IDE to open a project on Lombok, then you will be prompted to find methods of error, resulting in project compilation fails.

More importantly, if we define a jar package used in Lombok, then all applications require all rely on this jar package must install the plug-in, which is highly invasive.

Only times that I had decided not to use their own code in Lombok notes, but, in order to compile the project I will still use plug-ins Lombok.

3.2 code debugging reduced

Lombok can indeed help reduce a lot of code, because Lombok will help to automatically generate a lot of code.

However, these codes are to be generated only at compile time, so in the development process, in fact, a lot of code is actually missing.

This creates certain problems for code debugging, we want to know a property getter method of a class in which classes have been quoted, not so simple.

Affects Version 3.3 upgrade

Lombok codes for a strong invasive, it may bring a bigger issue that affects us upgrade the JDK.

Today, according to the frequency of the JDK upgrade every six months will launch a new version, but Lombok as a third-party tool, and is maintained by the open source team, his speed of iteration is not guaranteed.

So, if we need to upgrade to a new version of the JDK time, if one of the properties that are not supported in Lombok in words will be affected.

There may bring a problem, that is, Lombok own upgrade will be limited.

Because an application may depend on a number of jar package, and each jar package may have to rely on different versions of Lombok, which led to the application needs to be done in arbitration version, and we know, jar package versions arbitration is not so easy, and the probability of the occurrence of the problem is also high.

3.4 talk stepped pit

Lombok in use process, if the underlying principle for all kinds of annotations do not understand the words, it is easy to produce unexpected results.

Take a simple example:

We know that when we use @Data a class definition, will automatically help us generate equals () method.

But if only a @Data, without using @EqualsAndHashCode (callSuper = true), then by default is @EqualsAndHashCode (callSuper = false), this time generated equals () method compares only the subclass of property, will not be considered , regardless of the parent class property access is open from the parent class inherits the properties, which may get unexpected results.

3.5 could undermine encapsulation

If the above four points can be artificially avoid problems, then, on the issue of the package is a short board Lombok.

As a simple example, we define a shopping cart categories:

@Data
public class ShoppingCart { 
    //商品数目
    private int itemsCount; 
    //总价格
    private double totalPrice; 
    //商品明细
    private List items = new ArrayList<>();
}

We know that there is a relationship before the number of shopping carts, product details and the total price of the three, if you need to modify the words are to be modified together.

However, we used @Data notes in Lombok, and for itemsCount totalPrice these two properties, although we define them as private type, but provides a public getter, setter methods.

External setter methods can be modified freely by both value of the property, we can call arbitrary setter methods to re-set itemsCount, totalPrice value of the property, which can lead to inconsistency with items whose value property.

And object-oriented package is defined: by the access control, hide internal data, external interface to access only a limited class provided by modifying internal data. Therefore, the exposure setter method should not be exposed to a clear violation of the object-oriented characteristics of the package.

Good practice should not provide getter / setter, but only to provide a method addItem public, while taking modify itemsCount, totalPrice items as well as three properties.

4, summary

Lombok annotation can automatically generate code, greatly reducing the amount of code, the code is very simple.

But that does not mean the use of Lombok without any problems, the use of Lombok in the process, there may be unfriendly to his teammates, unfriendly code, debugging unfriendly, hostile upgrades and other issues.

Although the use of Lombok also lead to problems encapsulation of damage, but I think Lombok operation is followed Bean's use in mind.

Bean particular database and mapping Java classes Bean, Java is the definition and use of non-argument constructor and set and get methods of the Bean, and should not be treated any business in the bean have any logical relationship.

To conclude, my attitude towards Lombok is very vague, very clear advantages and disadvantages, adhering to the "do not use will have no problems" point of view, so I do not use, it will not take the initiative to recommend to others to use.

 

Shaoxia Please stay ...ヾ(◍ ° ∇ ° ◍) Techno゙... 
welcome thumbs up, comment, plus interest, to allow more people to see learn to earn
more exciting, please pay attention to my "Today's headlines No ": Java cloud notes

Published 171 original articles · won praise 311 · Views 100,000 +

Guess you like

Origin blog.csdn.net/weixin_44259720/article/details/104897398