Elegant encoding using Lombok
1、 Introduction and use
Lombok is a Java library that can simplify java code in the form of simple annotations and improve the development efficiency of developers.
Java beans that need to be written in the development process are commonly used. Development often takes time to add corresponding getters / setters, and perhaps write constructors, equals and other methods, which need to be maintained. When there are many attributes, there will be a large number of getter / setter methods, which are very lengthy and do not have much technical content. Once the attributes are modified, It is easy to forget to modify the corresponding method, which can be completed through Lombok's annotation, and! Lombok does more than that.
1、pom. xml
2. Plug in installation
It is recommended to install the idea Lombok plugin plug-in. It is not necessary, but it is slightly uncomfortable to see a lot of red errors during development~
2、 Annotation description
Here are some common Lombok annotations:
@Getter: getter method that automatically generates all properties.
@Setter: automatically generates setter methods for all properties.
@ToString: automatically generate tostring() method.
@Equalsandhashcode: generate equal () and hashcode () methods.
@Noargsconstructor: generates a parameterless constructor.
@Allargsconstructor: generates an all parameter constructor.
@Nonnull: generate non null check for field parameters to avoid NullPointerException;
@Requiredargsconstructor: generates a constructor for all required fields.
@Cleanup: used on local variables as the release of resources after use. For example, the file flow of FileInputStream will call the close () method after the use is completed.
@Sneakythlows: used on a method or constructor to throw an exception that needs to be thrown. For example, @ sneakythrows (unsupported encoding exception. Class).
@Synchronized: used on methods. This is equivalent to using the synchronized keyword on the method.
@Data: artifact, which is equivalent to using @ getter, @ setter, @ requiredargsconstructor, @ toString, @ equalsandhashcode at the same time.
@Slf4j: used on a class, which is equivalent to writing private static final org slf4j. Logger log = org. slf4j. LoggerFactory. getLogger(LogExample.class);
Complete annotation document description: https://projectlombok.org/features/all