关于Lombok和自动生成get set方法

在Java开发的项目里面免不了要用很多的get set 以及toString之类的方法,有时候确实是很繁琐而且做着重复共同工作,我们有没有办法来简化这个过程呢,当然有。

Lombok就可以很好的解决这个问题,它通过注解的方式来简化消除掉一些繁琐的Java代码,只在编译的过程中生成对应的方法,贴一下官网的介绍,由于是在编译的过程中生成的这些方法,所以是不会对运行的速度产生影响的。

Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

如何使用呢?

https://projectlombok.org/download官网或者github上面下载都可以。以eclipse为例,下载到安装目录下,cmd进目录下

java - jar lombok.jar

 就安装好了

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.14</version>
</dependency>

在pom.xml文件里面加入这些依赖就完成了,确实很简单,也很方便

不是web项目怎么自动生成呢?

有时候即使不做web香菇也会有很多这种重复代码,eclipse也有这些自动生成的功能

source>gennerate getter and setters,就可以实现这个功能了

猜你喜欢

转载自www.cnblogs.com/QuixoteY/p/10926238.html