Let people love and hate Lombok, in the end should not use

1 Introduction

Lombok, an island in Indonesia, is Longmu island. But in the world of Java, it is a convenient class library, which can provide a lot of convenience, so it is favored by many people. But there are many objections. Why is that?

I went to Longmu island to shoot the sunset.

2 convenience provided by Lombok

In general, when we use POJO in Java, it's easy to think of Lombok, such as VO, DTO, DO, etc. To use Lombok, you need to install the plug-in of the corresponding IDE, and introduce the dependency:


For example, if Lombok is not used, the number of code to implement getter/setter, equals, hashCode and toString is very large, as shown below:

package com.pkslow.basic.lombok;

import java.util.Objects;

public class Book {
    private String name;
    private int id;
    private double price;
    private String author;
    private String desc;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public int getId() {
        return id;

    public void setId(int id) {
        this.id = id;

    public double getPrice() {
        return price;

    public void setPrice(double price) {
        this.price = price;

    public String getAuthor() {
        return author;

    public void setAuthor(String author) {
        this.author = author;

    public String getDesc() {
        return desc;

    public void setDesc(String desc) {
        this.desc = desc;

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return id == book.id &&
                Double.compare(book.price, price) == 0 &&
                Objects.equals(name, book.name) &&
                Objects.equals(author, book.author) &&
                Objects.equals(desc, book.desc);

    public int hashCode() {
        return Objects.hash(name, id, price, author, desc);

    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", price=" + price +
                ", author='" + author + '\'' +
                ", desc='" + desc + '\'' +

And most of them are boilerplate code without much actual logic.

If Lombok is used, it is very simple. A annotation @ Data can do the above work (the method on the right can only be displayed after the Lombok plug-in is installed):

The common notes of Lombok are:

@NonNull: used for null pointer exception detection;

@Cleanup: automatic resource shutdown;

@Getter/@Setter: automatically generate get/set methods;

@ToString: generate toString method to facilitate printing and debugging;

@EqualsAndHashCode: generate equals and hashCode methods. Note that these two methods should be implemented at the same time;

@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor: construction method;

@Data: equivalent to @ toString +@ EqualsAndHashCode+@Getter+ @Setter +@RequiredArgsConstructor .

@Builder: build builder method;

@Log: log related:

@CommonsLog: org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@Flogger: com.google.common.flogger.FluentLogger.forEnclosingClass();
@JBossLog: org.jboss.logging.Logger.getLogger(LogExample.class);
@Log: java.util.logging.Logger.getLogger(LogExample.class.getName());
@Log4j: org.apache.log4j.Logger.getLogger(LogExample.class);
@Log4j2: org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
@Slf4j: org.slf4j.LoggerFactory.getLogger(LogExample.class);
@XSlf4j: org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
@CustomLog: com.foo.your.LoggerFactory.createYourLogger(LogExample.class);

So Lombok can really bring us great convenience, reduce a large number of repetitive, no business logic code, the code is relatively clean; modify the name of the field, no more changes; improve development efficiency.

3 problems brought by Lombok

Of course, different opinions have been put forward:

There is no specific code when debugging, which is not convenient for debugging;

It is necessary to force others to install the third-party plug-in, otherwise an error will be displayed;

When checking the test coverage, it is unable to show which codes are covered or not;

For example, toString method can't be implemented according to your own wishes. Sometimes it needs to output different String formats, not necessarily according to Lombok;

For some common methods, IDE can be generated automatically, and can be developed efficiently without Lombok.

IDEA does not provide Builder, but it can be used by installing plug-ins.

After successful installation, the Builder code can be generated:

4 Summary

At the beginning, I supported Lombok. After a period of use and some problems, I still think it is more suitable to generate code automatically through IDE. After all, it's too barbaric to force others to install plug-ins. If you generate code through IDEA, others will not install plug-ins or report errors.

In addition, more lines of code can reflect the workload, hahahaha

Welcome to Pumpkin slow down www.pkslow.com Get more!

Welcome to WeChat official account, "pumpkin slow talk", which will continue to update for you.

Read more, share more; write more, organize more.

Tags: Programming Lombok Java Apache log4j

Posted on Tue, 16 Jun 2020 02:12:32 -0400 by ShugNorris