1 introduction
Lombok is an island in Indonesia. But in the Java world, it is a convenient class library that offers a lot of convenience and is therefore favored by many people. But there are some objections. Why is that?
The sunset from lombok earlier.
2 Lombok’s convenience
When we use POJos in Java, it’s easy to think of Lombok, such as VO, DTO, DO, and so on. Using Lombok requires the installation of plug-ins for the IDE and the introduction of dependencies:
< the dependency > < groupId > org. Projectlombok < / groupId > < artifactId > lombok < / artifactId > < version > 1.18.10 < / version > <scope>provided</scope> </dependency>Copy the code
For example, implementing getters/setters, equals, hashCode, and toString without Lombok is a huge amount of code, like this:
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;
}
@Override
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);
}
@Override
public int hashCode() {
return Objects.hash(name, id, price, author, desc);
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", id=" + id +
", price=" + price +
", author='" + author + '\'' +
", desc='" + desc + '\'' +
'}';
}
}Copy the code
And it’s mostly boilerplate code, not much actual logic.
With Lombok it’s very simple, a single annotation @data does the job (the method on the right is displayed only after the Lombok plug-in is installed) :
Lombok’s common annotations are:
NonNull: for null pointer exception detection;
Cleanup: Automatic resource shutdown;
@getter / @setter: automatically generates get/set methods;
@toString: generate ToString method, easy to print debugging;
@equalSandHashCode: Generates equals and hashCode methods. Note that both should be implemented simultaneously;
@noargsConstructor, @requiredargsConstructor and @Allargsconstructor: constructors;
@data: equivalent to @toString + @EqualSandHashCode +@Getter+ @setter + @requiredargsConstructor.
Builder: Generate Builder method;
@log: Log related:
@ CommonsLog:org.apache.com mons. Logging. LogFactory. GetLog (LogExample. Class); Mon @ Flogger:com.google.com. 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. That the 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);Copy the code
So Lombok really does give us a lot of convenience, reducing a lot of repetitive, non-business logic code, the code is cleaner; Modified the name of the field without changing it more than once; Improve development efficiency.
3 Lombok’s problems
Of course, there are those who disagree:
Debugging without specific code, not convenient Debug;
You need to force others to install third-party plug-ins, otherwise an error will be displayed.
When checking test coverage, there is no intuitive way to show which code is covered and which is not;
The toString method, for example, sometimes needs to output a different String format, not necessarily the Lombok implementation;
For some common methods, ides can already be generated automatically and can be developed efficiently without Lombok.
IDEA does not provide a Builder, but it can be used by installing plug-ins.
After successful installation, you can generate the Builder code:
4 summarizes
I started out as a Lombok supporter, but after a while of use and some problems, I found it more appropriate to automate code generation through the IDE. After all, forcing people to install plug-ins is just too barbaric, while using IDEA to generate code, people do not install plug-ins will not have an error.
In addition, a few more lines of code can also reflect the workload, hahaha hahaha……
Welcome to visit www.pkslow.com for more wonderful articles!
Welcome to follow the wechat public account < pumpkin shuo >, will continue to update for you…
Read more, share more; Write more. Organize more.