1. Introduction to Lombok plug-in
Lombok is a Java development plug-in designed to eliminate tedious and tedious code in business engineering with some definition annotations, especially for simple Java Model Objects (POJOs). With the Lombok plugin, Java developers can save a lot of time repeatedly building methods such as hashCode and equals as well as accessors and ToString for various business object models. These methods are automatically generated during compilation of source code and do not reduce the performance of the program as reflected.
Lombok official website:《Project Lombok》
2. Install Lombok
(1) Add Maven dependencies
Open the file and add the Maven dependency:
<!-- Lombok rely --> <dependency> <groupId></groupId> <artifactId>lombok</artifactId> <version>1.18.34</version> <scope>provided</scope> </dependency>
(2) Install IDEA plug-in
Open IDEA, click the "File → settings" option in the IDEA menu bar, select the "Plugins" option in the pop-up dialog box, enter: Lombok in the search bar of the pop-up plug-in window, find Lombok in the search results, and click the "install" button to complete the installation. After the installation is complete, IDEA needs to be restarted before using Lombok.
3. The use of Lombok
After the installation is completed, developers can use simple annotations to help simplify and eliminate some necessary but seem bloated Java code, such as property constructors, getters, setters, equals, hashcode, toString methods, etc., thereby improving development efficiency and enabling developers to focus on the implementation of business logic.
【Example】Create entity classes and use the annotations provided by Lombok to simplify the code.
(1) Create user information entity class
package ; import ; import ; import ; /** * User information entity class * @author pan_junbiao **/ @Data @Builder @AllArgsConstructor @NoArgsConstructor public class UserInfo { private Long userId; private String userName; private String departmentCode; private String blogName; private String blogUrl; }
(2) Initialize the user information object class and print the object properties
public static void main(String[] args) { //Create user objects. Since using @Builder annotation, all objects can be created using chain style. UserInfo userInfo = () .userId(1L) .userName("pan_junbiao's blog") .blogName("Hello, welcome to pan_junbiao's blog") .blogUrl("/pan_junbiao") .build(); //Print the user object. Since the @Data annotation is used, all automatic generation of toString methods (userInfo); }
Console output result:
UserInfo(userId=1, userName=pan_junbiao's blog, blogName=Hello, welcome to pan_junbiao's blog, blogUrl=/pan_junbiao)
4. Lombok annotation instructions
annotation | illustrate |
---|---|
@Data | Automatically generate Getter/Setter, toString, equals, hashCode methods, and constructors without parameters. |
@Getter、@Setter | Automatically generate Getter/Setter method |
@ToString | Automatically generate the toString method. |
@NoArgsConstructor | Automatically generate parameterless construction method. |
@RequiredArgsConstructor | Automatically generate constructors that include attributes modified by final and @NonNull. |
@AllArgsConstructor | The construction method of automatically generating full parameters. |
@NonNull | Auxiliary handling NullPointerException exception. When used before using method parameters, it means that the parameter cannot be null when calling the method; when used above the property, it means that the value cannot be null when assigning the property. |
@EqualsAndHashCode | Automatically generate equals and hashCode and canEqual methods. Used to compare whether two class objects are the same. |
@Builder | Provides chain style creation objects. |
@CleanUp | Automatically manage resources, no longer need to add resources in finally, close methods, such as: close IO stream objects. |
@Value | Used to annotate the final class. |
@SneakyThrows | Snap and throw exceptions in the method. |
@Log | Supports the use of various log (Logger) objects. Generate log objects in the class and can be used directly in the method. For different log implementation products, there are different log annotations. @Log is used to indicate the use of the log function that comes with Java. In addition to @Log, you can also use @Log4j, @Log4j2, @Slf4j and other annotations to use different log products. |
This is the article about SpringBoot integrating Lombok plug-ins and instructions for using this article. For more related SpringBoot integrating Lombok content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!