Lombok Introduction and Basic use method

Source: Internet
Author: User

Lomboz is an Eclipse plugin based on LGPL's Open source Java development environment, supporting the lifecycle of software development such as coding, publishing, testing, and debugging, and supporting JSP,EJB. Lomboz is a Java EE plugin for Eclipse, which integrates many Java application servers, EE components, and Web application development into eclipse to help developers build, test, and deploy EE applications using Eclipse.

In http://www.blogjava.net/fancydeepin/archive/2012/07/12/lombok.html found to explain the installation of Lomboz article, watch the original can click on the link above, you can continue to read here, the content is consistent.

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 are on classes, provide getting and setting methods for all properties of a class, and also provide equals, canequal, Hashcode, toString methods
@Setter: Annotations on attributes; providing setting methods for attributes
@Getter: Annotations on attributes; providing getting methods for attributes
@Log4j: Annotations are on a class, and a log4j log object with a property named log is provided for the class
@NoArgsConstructor: Annotations on a class, providing an argument-free construction method for a class
@AllArgsConstructor: Annotations are on a class, and a method for constructing a class with full parameters

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){
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}

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}
13

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

Lombok Introduction and Basic use method

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.