First, what is Lombok?
Lombok is a small code generation tool. Official website: http://projectlombok.org/
The main features of Lombok are: automatic generation of default Getter/setter methods, automated resource management (through @cleanup annotations) and annotation-driven exception handling. Currently widely used in foreign countries.
Lombok It's like jquery, the goal is to get programmers to write less code, and to improve some of the original syntax. Lombok can do that. Neither with annotations process, nor with reflection. But directly black to the compilation process. So there is no effect on operational efficiency, we can validate it by decompile the class file.
Second, why the project to introduce Lombok
I think this is mainly the following three points:
1. Improve development efficiency
2. Make the code intuitive, concise, clear, reduce a large number of redundant code (generally can save 60%-70% more code)
3. Significantly reduce the cost of late maintenance
Third, how to use the Lombok
Currently we are mainly using eclipse development, so take eclipse as an example to demonstrate:
1. Installation of Lombok on Eclipse
Java-jar Lombok.jar
To install the Eclipse plug-in
2. Use of Lombok
The Lombok feature is to create some code based on annotation to reduce the number of duplicate code, which provides the following annotation:
@Getter and @setter: Creating getter and Setter for attributes
@EqualsAndHashCode: Implementing the Equals () method and the Hashcode () method
@ToString: Implementing the ToString () method
@Data: Above 3 annotation and, will create Getter Setters equals Hashcode and ToString
@Cleanup: Close the stream
@Synchronized: Object synchronization
@SneakyThrows: Throwing an exception
@Log4j: log4j Log Declaration
Now, let's look at some examples:
@Getter @Setter
The original wording:
The writing in Lombok
When generating getter/setter methods, Lombok comply with traditional standards. All of these method names begin with a GET or set and the property names are uppercase. Of course, if the attribute is a Boolean, the exception is the case. In this case, the getter starts with IS, not get. This is a standard practice for Java beans;
Hashcode when JavaBean output number;
ToString output class and a printed list of each property;
Access permissions for the Modifier getter method
@Getter (accesslevel.protected) to modify the access rights of the Getter method
@Setter (accesslevel.protected) modifier Setter Method access rights
@Getter (accesslevel.protected) private int size;
@ToString
To exclude a particular field from overriding the ToString method:
@ToString (exclude= "Color")
Public @Data class Lure {
private String name;
private int size;
private String color;
private String style;
}
@EqualsAndHashCode
Wait a minute...... It is very convenient to use.