Lombok installation and entry-level lombok Installation

Source: Internet
Author: User

Lombok installation and entry-level lombok Installation
Preface:

I did not intend to find it when I visited the open-source community. After a while, I thought it was okay. I would like to recommend it.
LombokA simple annotation form is provided to help us simplify and eliminate some of the necessary but bloated java code. Especially compared with POJO, it is not my style. Let's take a look.

Lombok Official Website: http://projectlombok.org/

Lombok has actually been introduced here. I am joking. In fact, there is a video of lombok at 3 minutes 49 seconds on the official website, which also gives a clear explanation. There are also documents for reference.
I won't talk too much here. Let's take a look.Lombok InstallationIn fact, this video on the official website has also been mentioned.

Lombok Installation
Lombok must be installed. If it is not installed, IDE cannot parse the lombok annotation. Download the latest JAR package from the official website. It is 0.11.2 and I use 0.11.0.
When I first used it, I downloaded the latest version, that is, the 0.11.0 version I used. Now I have updated two versions. The update is fast ......

1. Double-click the downloaded JAR package to install lombok
When I select this method for installation, the system prompts that no IDE is found, so I did not install it successfully. I installed it manually. If you want to install it in this way, see the video on the official website.

2. Manually install lombok in eclipse/myeclipse
1. Copy lombok. jar to the directory where myeclipse. ini/eclipse. ini is located.
2. Open eclipse. ini/myeclipse. ini, insert the following two lines at the end, and save:
-Xbootclasspath/a: lombok. jar
-Javaagent: lombok. jar
3. Restart eclipse/myeclipse

Lombok annotation:
Lombok does not provide many annotations. You can refer to the official video and official documents.
Lombok annotation online help documentation: http://projectlombok.org/features/index.
The following describes some frequently-used lombok Annotations:
@ Data: Annotation on the class; provides the getting and setting methods for all attributes of the class, and also provides the equals, canEqual, hashCode, and toString methods.
@ Setter: Annotation on the property; provides the setting method for the property
@ Getter: Annotation on the property; provides the getting method for the property
@ Log4j: Annotation on the class; provides a log4j log object named log for the class.
@ NoArgsConstructor: Annotation on the class; provides a construction method without parameters for the class
@ AllArgsConstructor: Annotation on the class; provides a constructor for the class.

The following is a simple example.
1. Do not use lombok Solution

1
2 public class Person {
3
4 private String id;
5 private String name;
6 private String identity;
7 private Logger log = Logger. getLogger (Person. class );
8
9 public Person (){
10
11}
12
13 public Person (String id, String name, String identity ){
14 this. id = id;
15 this. name = name;
16 this. identity = identity;
17}
18
19 public String getId (){
20 return id;
21}
22
23 public String getName (){
24 return name;
25}
26
27 public String getIdentity (){
28 return identity;
29}
30
31 public void setId (String id ){
32 this. id = id;
33}
34
35 public void setName (String name ){
36 this. name = name;
37}
38
39 public void setIdentity (String identity ){
40 this. identity = identity;
41}
42}
43


2. lombok Solution

1
2 @ Data
3 @ Log4j
4 @ NoArgsConstructor
5 @ AllArgsConstructor
6 public class Person {
7
8 private String id;
9 private String name;
10 private String identity;
11
12}
13

The above two java classes have the same effect in terms of function. In comparison, it is obvious that lombok is much more concise, especially when there are many class attributes,
At the same time, it also avoids the low-level errors caused by modifying the method name when modifying the field name. Note that when using the lombok annotation, remember to import the lombok. jar package to the project.

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.