Eight: Lombok installation, Getting started-eliminate lengthy Java code

Source: Internet
Author: User
Tags log4j

Lombok installation, getting started-eliminate lengthy Java code


Objective:
Visit the open source community when the unintentional discovery, with a period of time, think can also, hereby recommended.
LombokProvides a simple form of annotations to help us simplify the removal of some of the necessary but bloated Java code. In particular, compared to POJO, Light said not to do is not my style, first to see it.

Lombok's official website: http://projectlombok.org/

Lombok actually here I introduced, open a joke, in fact, the official online Lombok 3 minutes 49 seconds of video explanation, Inside said is also very clear, and there are documents to reference.
I don't talk too much here, let's take a look.installation of Lombok, in fact, this official website video has also talked about

Lombok Installation
Using Lombok is required, and if not installed, the IDE cannot parse the Lombok annotations. First download the latest version of the JAR package on the official website, it is now 0.11.2 version, I use 0.11.0
The first time I used the download is the latest version, that is, I now use 0.11.0, up to now has updated two versions, updated very fast ah ...

1. Double-click the downloaded JAR package to install Lombok
I chose to install this way when prompted not to find any IDE, so I did not install successfully, I was installed manually. If you want to install this way, please refer to the website's video.

2.eclipse/myeclipse Manual Installation Lombok
1. Copy the Lombok.jar to the folder 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 Eclipse/myeclipse

Lombok notes:
Lombok offers a few annotations, and can refer to the official video commentary and official documentation.
Lombok Annotations Online Help documentation: Http://projectlombok.org/features/index.
Here are a few of the Lombok annotations I commonly use:
@Data: Annotations on classes; provides getting and setting methods for all properties of a class, plus equals, canequal, Hashcode, toString methods
@Setter: annotations are on attributes; Provides setting method for attributes
@Getter: annotations are on attributes; provides getting method for attributes
@Log4j: Annotations on a class; Provides a log4j log object with a property named log for the class
@NoArgsConstructor: Annotations on a class; provides an argument-free construction method for a class
@AllArgsConstructor: Annotations are on a class; Provides a method for constructing a full parameter for a class

@Getter/@Setter

Can be used on classes and attributes, placed on a class, generates a Getter/setter method on all non-static (Non-static) properties, and on the property, generates Getter/setter methods on the property. You can also specify the access level of the Getter/setter method.

@EqualsAndHashCode

By default, all non-transient (non-transient) and non-static (non-static) fields are used to generate the Equals and Hascode methods, or you can specify which properties are used specifically.

@ToString

Generates the ToString method, which, by default, outputs the class name, all properties, and the attributes are output in sequential order, separated by commas.

@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor

The parameterless constructor, the partial parameter constructor, and the full parameter constructor, Lombok is powerless when we need to overload multiple constructors.

@Data

@ToString, @EqualsAndHashCode, the @getter of all attributes, the combination of @setter and @requiredargsconstructor of all non-final properties, usually we use this annotation as sufficient.



Here is a simple example
1. Scenarios that do not use Lombok

1
2public 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
Public person (string ID, string name, String identity){
This.id = ID;
THIS.name = name;
This.identity = identity;
17}
18
Public String getId (){
return ID;
21}
22
All public String getName (){
return name;
25}
26
Public String getidentity (){
return identity;
29}
30
public void SetId (String ID){
This.id = ID;
33}
34
public void SetName (String name) {
THIS.name = name;
37}
38
setidentity public void (String identity) {
This.identity = identity;
41}
42}
+


2. Scenarios for using Lombok

1
2@data
3@log4j
4@noargsconstructor
5@allargsconstructor
6public class Person{
7
8 Private String ID;
9 private String name;
Ten private String identity;
11
12}
-


The above two Java classes, from the point of view, their effect is the same, compared with, it is clear that the use of Lombok is much more concise, especially in the case of the class of the property more,
It also avoids the low-level errors in modifying the name of the field when you forget to modify the method name. Finally, it is important to remember to import the Lombok.jar package to the project when using Lombok annotations

Eight: Lombok installation, Getting started-eliminate lengthy Java code

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.