This is the 13th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
preface
Let’s start with a common piece of code. :
public class Person {
private String name;
private int age;
private String sex;
private String birthday;
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
// Omit multiple setters and getters
Copy the code
This is our most common code, each private decorated member variable has a setter and getter method, but the disadvantage is obvious, is that the setter/getter method increases as the field increases, which makes the entity class look bloated and visually unattractive.
So Lombok is a tool that automatically generates setters and getters for you! With a few comments, the code above can look like this!
@Setter
@Getter
public class Person {
private String name;
private int age;
private String sex;
private String birthday;
}
Copy the code
It’s so refreshing, it’s so simple! So maybe we’re going to start the bar, so why don’t we just use public, so we don’t have to write setters and getters, what else do we use? But the specification is the specification, in order to control the access to variables in their own hands, so it must be decorated with private. Next comes amway Lombok usage and other features.
IDEA Install plug-in
In the Settings Plugins, search Lombok and install it. Restart IDEA to take effect.
Introduction of depend on
If you are using the Maven project, just import its POM:
< the dependency > < groupId > org. Projectlombok < / groupId > < artifactId > lombok < / artifactId > < version > 1.18.12 < / version > </dependency>Copy the code
Import Lombok-1.18.12.jar directly without using Maven.
Use of
Write some code that includes common and easy-to-understand Lombok annotations
@Setter
@Getter
@Slf4j
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private String name;
private int age;
private String sex;
private String birthday;
public void sing(@NonNull String songName) {
// @slf4j generates logger
log.info(songName);
System.out.println(songName);
}
public void read(String bookPath) throws FileNotFoundException {
@Cleanup
FileInputStream in = newFileInputStream(bookPath); }}Copy the code
@ Setter and @ Getter
These two annotations are used on a class to add getters and setters for all private member variables, and on member variables to add setters and getters for individual member variables
@Slf4j
When we use SLF4J to print logs, we need to obtain logger. The code is as follows:
Logger logger = LoggerFactory.getLogger(Person.class);
logger.info("Hello World!")
Copy the code
With the @slf4J annotation, Lombok helps you create a logger called log that you can use directly.
@ToString
This is not recommended, although it will help you generate toString methods, but it is often not suitable for our purposes. His output looks something like this.
Person(name=null, age=0, sex=null, birthday=null)
Copy the code
@AllArgsConstructor
Help you generate a constructor that contains all the fields
@NoArgsConstructor
New Person(){}
@NotNull
As the name implies, the variables and parameters it modifies cannot be null. Test the
public static void main(String[] args) {
new Person().sing(null);
}
/ / error: the Exception in the thread "is the main" Java. Lang. NullPointerException: songName is marked non - null but is null
Copy the code
@Cleanup
We create the stream while reading the file and always call close() to close the stream when we’re done using it.
When the database connection is obtained, close() is also called to close the connection.
The @cleanup decorator will automatically close the stream and Connection when it is finished, so you don’t have to call close() to close it
@Data
This is not included in the sample code because this is a set of @setter, @getter, @slf4j applied to the class. But it is not recommended.
The included annotations are shown below:
conclusion
Lombok simplifies code, but it also introduces some barriers to code reading. Weigh the pros and cons. Every coin has two sides, some people love it, some hate it. From my perspective, at least, it makes my code cleaner. It’s rumored that Java14 will also offer these Lombok features, so we’ll see.