Java Lombok Reduce Code Redundancy razor

Source: Internet
Author: User

Objective:

It is now the era of the ORM framework, with its heavyweight framework such as Hibernate, which provides bidirectional mapping of java<-> data, as well as a lightweight framework such as MyBatis, which provides a single-directional mapping of java<-data,

Regardless of which popular framework is used, in which the model class is written, as well as the intricate and responsible getter/setter method, the existence of these getter/setter methods makes the code very bloated. So how do you

Solve this problem, then introduced the protagonist of this article Lombok.

1. Download:

Lombok's official address is: http://projectlombok.org/

1. Function:

Lombok is a javaarchive (jar) file designed to reduce redundancy when encoding.

Automatic installation Method:

Run the Lombok.jar file directly, first of all to ensure that the machine's Java environment is configured properly, after the automatic installation is completed, will achieve the same effect as manual installation.

Manual Installation Method:

1. Copy the Lombok.jar to the file directory where the Myeclipse.ini/eclipse.ini resides.

2. Open Eclipse.ini/myeclipse.ini, insert the following two lines on the last side and save:

-xbootclasspath/a:lombok.jar

-javaagent:lombok.jar

3. Restart Myeclipse/eclipse

Note: This is an official installation method, when using MyEclipse, according to the above method can not be used normally, you need to change the MyEclipse executable name from Myeclipse.exe to

Eclipse.exe, it is only normal to use this time (the reason is not because Lombok in the internal specified some constants, but also undecided).

Steps to use in the IDE:

After you install the Lombok.jar file in the normal steps, you need to introduce the Lombok.jar file in your project and add it to the classpath path.

3. Detailed:

Lombok is based on annotation completion tags, and then special parsing of tagged variables, Lombok contains some of the following custom annotations:

@val/@NonNull/@Cleanup/@Getter/@Setter/@ToString/@EqualsAndHashCode/@NoArgsConstructor/@ Requiredargsconstructor and @AllArgsConstructor

/@Data/@Value/@Builder/@Synchronized/@Getter (lazy=true)/@Log, Lombok provides these annotations to complete the streamlining of the code.

Lombok can do less coding in the IDE, neither with annotations process nor reflection, but directly in the compiler's process of compiling the original code.

Specific examples are as follows:

Use Lombok's Mmodel class:

Public @Data class User {
private String name;

}

Or

public class User {
@Getter @Setter
private String name;

}

At this point: if you can see in the IDE's outline window The Getter/setter method that generates the name directly in the bytecode, which greatly simplifies the amount of code in the programming process, and Lomnok does not need third-party dependencies to complete

The operation for the model class.

In other classes, you can use the same class as normal:

User user = new user ();
User.setname ("WANGBC");

user.getname ();

4. Annotation meaning:

• @Data: Equivalent to simultaneous use of @string, @EqualsAndHashCode, @Getter, @Setter, and @requiredargsconstrutor These annotations are useful for Pojo classes

• @NonNull: Adding this annotation to the method parameter automatically checks whether the parameter is empty within the method, and if it is empty, throws the NPE

• @CleanUp: Automatically generate code such as try-finally to close the stream

• @Getter (lazy=true): Can replace the classic double Check lock boilerplate code.

• Others can refer to the official website documentation for more information.

5. Insufficient:

Although Lombok can streamline the code, it still encounters problems in its use: for example, the hiding of code in the source makes it more difficult to debug the code, increasing the hassle of code refactoring, and

Lombok is not yet perfectly compatible with eclipse in the process of use, which is an issue that needs to be considered in practical applications, but I think it is harmless.

6. End:

Lombok is a small, but useful, open source project that can drastically reduce the code redundancy that results from an ORM framework application. In any case, there is no use in the source code that is nearing the completion of project

Code can be less readable, and fewer codes will be easier to maintain.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Lombok Reduce Code Redundancy razor

Related Article

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.