Detailed use of Lombok

Source: Internet
Author: User

Lombok usage

Introduction:
Using Lombok in a project can reduce the writing of a lot of repetitive code. For example, compile the getter/setter/tostring method.

Introduce dependency:
Add the Lombok dependency jar to the project and add the following parts to the POM file. (It is unclear that the version can be searched in the maven repository)

1 <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->2     <dependency>3         <groupId>org.projectlombok</groupId>4         <artifactId>lombok</artifactId>5         <version>1.16.18</version>6         <scope>provided</scope>7     </dependency>

Usage:
Use the corresponding annotation on the corresponding class or method.

What are Lombok Annotations:
@ Setter
@ Getter
@ Data
@ Log (this is a generic annotation, which has many forms)
@ Allargsconstructor
@ Noargsconstructor
@ Override sandhashcode
@ Nonnull
@ Cleanup
@ Tostring
@ Requiredargsconstructor
@ Value
@ Sneakythrows
@ Synchronized

@ Getter and @ setter

This annotation is used on classes or attributes. This annotation can be used on classes or attributes. The generated getter follows the conventions of the Boolean attribute. For example, for a Boolean sex, the getter method is issex instead of getsex.

When this annotation is used, a parameter-free structure is generated by default. And the corresponding getterhe setter Method

This annotation can also be used on a single attribute. A parameter-free structure is generated by default.

@ Data

This annotation is used on the class. This annotation provides the getter, setter, equals, canequal, hashcode, and tostring methods.

@ Nonnull

This annotation is used in attributes. This annotation is used for non-null checks of the genus. When it is placed on the field of the setter method, an empty check is generated. If it is null, nullpointerexception is thrown.
This annotation generates a construction without parameters by default.

@ Tostring

This annotation is used on the class. By default, any non-platform field is generated and output in the form of name-value.
1. If necessary, you can use the comment parameter includefieldnames to control whether the output contains the attribute name.
2. You can use the exclude parameter to include the field name and exclude specific fields from the generated method.
3. The callsuper parameter can be used to control the output of the parent class.

Callsuper output parent class attributes
Note: the parent class must also have the tostring method. Otherwise, the object memory address will be printed.
For example:
// No tostring method for the parent class
Person ([email protected], firstname = motui, address = Beijing, dateofbirth = Tue Jan 09 11:49:05 CST 2018, sex = true)
// The parent class has the tostring method.
Person (Super = people (ID = 111), firstname = motui, address = Beijing, dateofbirth = Tue Jan 09 11:50:11 CST 2018, sex = true)

@ Override sandhashcode

The annotation is used on the class. The annotation generates both equals and hashcode at the class level annotation.
Note the usage of this annotation when inheriting the relationship. For details, refer to the official introduction.
The callsuper parameter must be set to true if there is an inheritance relationship.

@ Data

This annotation is used in the class. This annotation is the most commonly used annotation. It combines @ tostring, @ javassandhashcode, @ getter, and @ setter. Essentially, @ data annotation is used. By default, @ tostring and @ javassandhashcode are used for the class, and each field contains @ setter and @ getter. This annotation also generates a public constructor that can take any @ nonnull and final fields as parameters.

Although the @ data annotation is very useful, it does not have the same control granularity as other annotations. @ Data provides a single parameter that can generate a static factory. Set the staticconstructor parameter to the desired name, And the automatically generated Lombok constructor is set to private, it also provides a public static factory method with a given name.
Equivalent to changing the created constructor to private, and then providing a public static factory method with a given name.

@Data(staticConstructor = “PersonFactory”) public class Person{ ……}private person(){}public static Person PersonFactory(){ return new Person; }

  

@ Allargsconstructor

This annotation is used on the class. This annotation provides a full parameter constructor. By default, no parameter constructor is provided.

@ Noargsconstructor

This annotation is used on the class. This annotation provides a construction without parameters.

@ Requiredargsconstructor

This annotation is used in the class to generate the corresponding constructor using all member variables with @ nonnull annotation or with final modifier in the class.

@ Value

This annotation is used in the class and will generate a constructor containing all the parameters. The get method also provides the equals, hashcode, and tostring methods.
Note: No setter

@ Cleanup

This annotation is used before attributes to ensure that the allocated resources are released. Use this annotation on local variables. Any subsequent code will be encapsulated in try/finally to ensure that the resources currently acting on are released. The default @ cleanup method is close. You can use value to specify different method names.

@ Sneakythrows

This annotation is used in methods. This annotation is used in methods. You can wrap the code in the method with a try-catch statement to catch exceptions and use Lombok in catch. sneakythrow (e) throws an exception. You can use @ sneakythrows (exception. class) to specify the exception thrown. This annotation must be used with caution.

@ Synchronized

This annotation is used in a class or instance method, and synchronized is used in a method. Using keywords may lead to different results than expected results, because exceptions may occur in multithreading. Synchronized
The keyword will lock the current object in the case of this sample method, or lock more on the object of the class platform method. This may cause deadlocks. In general, it is recommended to lock an independent lock dedicated for this purpose, rather than allowing public objects to be locked. This annotation is also for this purpose.

Detailed use of Lombok

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.