Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of using Lombok for Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the use of Lombok in Java". In daily operation, I believe that many people have doubts about the use of Lombok in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "what is the use of Lombok in Java?" Next, please follow the editor to study!

Preface

In the process of project development, some adjustments are often involved, such as Getter/Setter method of entity class, ToString method and so on. At this point, you can use Lombok to avoid this repetitive operation, reduce the bloated non-core code, and improve coding efficiency.

How to introduce Lombok into IntelliJ IDEA

Install the Lombok plug-in (otherwise IDE will prompt for an error when calling the setter/getter method): File-> Settings-> Plugins search Lombok Plugin to complete the installation.

Lombok dependency is introduced into pom.xml (where provided means to rely on the jar only during compilation and test, but not at run time (if the jar is already included in the run container, it is set to provided to avoid jar conflicts), and the default compile means to rely on it during compilation, test and run):

Org.projectlomboklombok1.16.20provided

The use of Lombok annotations

@ Getter/@Setter

Generate Getter and Setter methods for fields that can be annotated to a field or class (annotations generate Getter and Setter methods on the class for all fields in the class). The default is public, and you can modify the access level of the method if necessary: @ Getter (AccessLevel.PROTECTED)

Annotations in Lombok typically include a no-parameter constructor annotation @ NoArgsConstructor (used to generate a no-parameter constructor), so an additional no-parameter constructor is generated.

@ NonNull

When calling the setter method of a field, if the parameter passed is null, an empty exception NullPointerException will be thrown, and the setter method will be generated to check whether the parameter is empty.

@ NoArgsConstructor

Generate a no-parameter constructor. When there is a final field in the class that is not initialized, the compiler reports an error, using @ NoArgsConstructor (force = true), and then sets the default value of 0 / false / null for the uninitialized final field so that the compiler does not report an error. For fields with constraints, such as the @ NonNull field, no checks or assignments are generated, so note that these constraints are not valid until these fields are initialized correctly.

@ RequiredArgsConstructor

Generate a constructor (with or without parameters), which can only be an uninitialized field decorated with final or an uninitialized field annotated with @ NonNull.

@ RequiredArgsConstructor (staticName = "of") generates a static method of of () and sets the constructor to private

@ AllArgsConstructor

A construction method for generating a full parameter

@ ToString

Generate the toString () method, which by default prints your class name and each field in order (separated by commas). You can set which fields are not included, you can specify one or multiple @ ToString (exclude = "id") / @ ToString (exclude = {"id", "name"}). If you inherit a parent class, you can set callSuper to call the parent class's toString () method, for example: @ ToString (callSuper = true)

@ EqualsAndHashCode

Generate the hashCode () and equals () methods, which by default use all non-static, non-transient fields. However, you can exclude more fields by including the optional exclude parameter. Alternatively, specify exactly which fields you want to use by naming them in the of parameter.

/ / exclude exclusion field @ EqualsAndHashCode (exclude = {"password", "salt"}) / / of specifies the field @ EqualsAndHashCode to be included (of = {"id", "phone", "password"})

@ Data

@ Data includes @ ToString, @ EqualsAndHashCode, @ Getter / @ Setter and @ RequiredArgsConstructor functions

@ Value

@ Value makes fields immutable: decorated with final, but also includes @ ToString, @ EqualsAndHashCode,

@ AllArgsConstructor, @ Getter (note that only Getter does not have Setter)

@ Log

Generate a log object for logging. You can use the topic attribute to set the parameters of the getLogger (String name) method, such as @ Log4j (topic = "com.xxx.service.xxx"). The default is the fully qualified name of the class, that is, the class name .class. Log supports the following:

@ Log java.util.logging.Logger @ Log4j org.apache.log4j.Logger @ Log4j2 org.apache.logging.log4j.Logger @ Slf4j org.slf4j.Logger @ XSlf4j org.slf4j.ext.XLogger @ CommonsLog org.apache.commons.logging.Log @ JBossLog org.jboss.logging.Logger

@ Logprivate static final java.util.logging.Logger log = java.util.logging.Logger.getLogger (LogExample.class.getName ()); @ Log4jprivate static final Logger log = org.apache.log4j.Logger.Logger.getLogger (UserService.class); @ Log4j2private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger (LogExample.class); @ Slf4jprivate static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger (LogExample.class) @ XSlf4jprivate static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger (LogExample.class); @ CommonsLogprivate static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog (LogExample.class); @ JBossLogprivate static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger (LogExample.class)

@ SneakyThrows

Use the try catch modifier method to catch exceptions. The Throwable exception is caught by default, or you can set the exception to be caught: @ SneakyThrows (InterruptedException.class)

@ Synchronized

Add a synchronization lock to a method

@ Cleanup

It is mainly used to modify IO stream-related classes, and the resource will be close () in the finally code block.

@ Getter (lazy = true)

The callout field is a lazily loaded field. The lazily loaded field is not initialized when the object is created, but will only be initialized when it is accessed for the first time, and will not be initialized again later.

@ Wither

Provides a method of assigning values to final fields

@ Builder

Generate a complex builder API for your class.

@ Delegate

Generate a bunch of common methods for fields of type List, all of which are actually methods in List. Note: only one @ Delegate annotation can be used in a class, because using multiple will generate multiple size () methods, which will compile the error message.

At this point, the study on "what is the use of Lombok in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report