177. Spring Boot lombok: Overview + Installation

 

【Video & Communication Platform】

à SpringBoot Video 

http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à  SpringCloud Video

http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à Spring Boot source code 

https://gitee.com/happyangellxq520/spring-boot

à Spring Boot communication platform 

http://412887952-qq-com.iteye.com/blog/2321532

 

Origin of demand

In the development process, we usually define a large number of JavaBeans , and then use the IDE to generate the constructor, getter , setter , equals , hashcode , toString methods of its properties. When we want to change a property, such as naming, type, etc. , all need to regenerate these methods mentioned above . Is there a way in Java to avoid this duplication of labor? Lombok was born for this.

Outline of this section:

1. Introduction to Lombok

2. Lombok installation

3. Annotation introduction

 

1. Introduction to Lombok

Lombok is a tool that can help us simplify and eliminate some necessary but bloated Java code through a simple form of annotation . By using the corresponding annotation , the corresponding method can be generated when compiling the source code .

Official address: https://projectlombok.org/ .

github address: https://github.com/rzwitserloot/lombok .

2. Lombok installation

2.1 下载lombok.jar

       从地址https://projectlombok.org/download 下载lombok.jar,当前最新的版本是1.16.18

2.2  运行lombok.jar

       运行Lombok.jarjava -jar D:\lombok.jarD:\lombok.jar这是windowslombok.jar所在的位置)。

数秒后将弹出一框,以确认IDE的安装路径(选择IDE的路径,这里使用的Eclipse

确认完eclipse的安装路径后,点击install/update按钮,即可安装完成

2.3  验证是否安装成功

安装完成之后,请确认eclipse安装路径下是否多了一个lombok.jar包,并且其

配置文件eclipse.ini中是否 添加了如下内容: 
    -javaagent:lombok.jar 
    -Xbootclasspath/a:lombok.jar 

如果上面的答案均为true,那么恭喜你已经安装成功,否则将缺少的部分添加到相应的位置即可。

2.4  重启IDE

       重启IDE,这里是重启eclipse,到这里就可以体验lombok带来的快乐了。

 

三、注解介绍

       在这里介绍几个常用的注解,更多的可以参考:https://projectlombok.org/features/index.html

 

1@Getter / @Setter

可以作用在类上和属性上,放在类上,会对所有的非静态(non-static)属性生成Getter/Setter方法,放在属性上,会对该属性生成Getter/Setter方法。并可以指定Getter/Setter方法的访问级别。

2@EqualsAndHashCode

默认情况下,会使用所有非瞬态(non-transient)和非静态(non-static)字段来生成equalshascode方法,也可以指定具体使用哪些属性。

3@ToString

生成toString方法,默认情况下,会输出类名、所有属性,属性会按照顺序输出,以逗号分割。

4@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor

无参构造器、部分参数构造器、全参构造器,当我们需要重载多个构造器的时候,Lombok就无能为力了。

5@Data

@ToString, @EqualsAndHashCode, 所有属性的@Getter, 所有non-final属性的@Setter@RequiredArgsConstructor的组合,通常情况下,我们使用这个注解就足够了。

本篇文章参考如下文章

eclipse中使用Lombokhttps://segmentfault.com/n/1330000003805656

Java奇淫巧技之Lombokhttp://blog.csdn.net/ghsau/article/details/52334762

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326462066&siteId=291194637