Lombok - 消除冗长的 java 代码

</pre><span style="font-size:18px"><span style="line-height:19.200000762939453px; font-family:Georgia; color:red">lombok 的官方网址:</span><span style="line-height:19.200000762939453px; font-family:Georgia"><span style="color:#1d58d1"></span><a target=_blank target="_blank" href="http://projectlombok.org/"><span style="color:#1d58d1">http://projectlombok.org/</span>  </a>lombok 其实到这里我就介绍完了,开个玩笑,其实官网上有 lombok 三分四十九秒的视频讲解,里面讲的也很清楚了,而且还有文档可以参考。</span></span><p><span style="line-height:19.200000762939453px; font-family:Georgia"><span style="font-size:18px">在这里我就不扯太多,先来看一下<strong> lombok 的安装</strong>,其实这个官网视频上也有讲到啦</span></span></p><h1><span style="font-size:10pt; line-height:19.200000762939453px; font-family:Georgia"></span></h1><h1><span style="line-height:19.200000762939453px; font-family:Georgia"><span style="line-height:19.200000762939453px; font-family:Georgia; color:red"><span style="font-size:18px">lombok 安装</span></span></span></h1><p><span style="line-height:19.200000762939453px; font-family:Georgia"><span style="line-height:19.200000762939453px; font-family:Georgia"><span style="font-size:18px">    使用 lombok 是需要安装的,如果不安装,IDE 则无法解析 lombok 注解。先在官网下载最新版本的 JAR 包,现在是 1.14.8 版本</span></span></span></p><p><span style="font-size:10pt; line-height:19.200000762939453px; font-family:Georgia"><span style="font-size:10pt; line-height:19.200000762939453px; font-family:Georgia"></span></span></p><h2><span style="line-height:19.200000762939453px; font-family:Georgia"><span style="line-height:19.200000762939453px; font-family:Georgia"><span style="color:rgb(255,0,0); font-family:Georgia; line-height:19.200000762939453px"><span style="font-size:18px">一.<span style="color:rgb(255,0,0); font-family:Georgia; line-height:19.200000762939453px">安装</span>下载下来的<span style="color:rgb(255,0,0); font-family:Georgia; line-height:19.200000762939453px">lombok  </span> JAR 包 </span></span></span></span></h2><div><span style="line-height:19.200000762939453px; font-family:Georgia"><span style="line-height:19.200000762939453px; font-family:Georgia"><span style="font-family:Georgia; line-height:19.200000762939453px"></span></span></span><p style="line-height:28px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both"><strong><span style="font-size:16px; line-height:1.6em; font-family:'AR PL UKai CN'">1、下载Lombok.jar </span><a target=_blank target="_blank" rel="nofollow" href="http://projectlombok.googlecode.com/files/lombok.jar" style="font-size:12px; line-height:1.6em; text-decoration:none; font-family:'AR PL UKai CN'; outline:none; zoom:1">http://projectlombok.googlecode.com/files/lombok.jar</a><span style="font-size:16px; line-height:1.6em; font-family:'AR PL UKai CN'"> </span><br style="line-height:1.6em" /><span style="font-size:16px; line-height:1.6em; font-family:'AR PL UKai CN'">2、运行Lombok.jar: 在lombok.jar的目录 运行:</span></strong></p><p style="line-height:28px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both"><span style="font-family:'AR PL UKai CN'"><span style="font-size:14px"><strong>   <span style="color:#000099">$</span></strong></span><span style="font-size:24px; color:#000099"><strong>java -jar lombok.jar</strong></span></span></p><p style="font-size:16px; line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><strong>   数秒后将弹出一框,以确认eclipse的安装路径</strong>:</p><p style="font-size:16px; line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"></p><p style="font-size:16px; color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><img src="https://img-blog.csdn.net/20141214214406461?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXVhbmxhaXNoaW5pMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></p><p style="font-size:16px; color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"></p><p style="font-size:16px; color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="color:rgb(68,62,57); font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:25.600000381469727px; background-color:rgb(249,247,241)"></span></p><p style="font-size:16px; color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="color:rgb(68,62,57); font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:25.600000381469727px; background-color:rgb(249,247,241)"></span></p><p style="font-size:16px; color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="color:rgb(68,62,57); font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:25.600000381469727px; background-color:rgb(249,247,241)"><strong>3、确认完eclipse的安装路径后,点击install/update按钮,即可安装完成</strong></span></p><p style="font-size:16px; color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="color:rgb(68,62,57); font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; line-height:25.600000381469727px; background-color:rgb(249,247,241)"></span></p><p style="font-size:16px; color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><img src="https://img-blog.csdn.net/20141214214644942?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXVhbmxhaXNoaW5pMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></p><p style="font-size:16px; color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"></p><p style="font-size:16px; color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><img src="https://img-blog.csdn.net/20141214214922612?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXVhbmxhaXNoaW5pMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></p><p style="font-size:16px; color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"></p><p style="line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="font-size:18px"></span></p><p style="line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"></p><p style="line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="line-height:1.6em"><span style="font-size:18px">4、安装完成之后,请确认eclipse安装路径下是否多了一个lombok.jar包,并且其</span></span></p><p style="line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="line-height:1.6em"><span style="font-size:18px">     配置文件eclipse.ini中是否 添加了如下内容:</span></span></p><p style="line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="font-size:18px"><span style="line-height:1.6em">           </span>-javaagent:lombok.jar</span></p><p style="line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="font-size:18px">           -Xbootclasspath/a:lombok.jar</span></p><p style="line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="font-size:18px">     如果上面的答案均为true,那么恭喜你已经安装成功,否则将缺少的部分添加到相应的位置即可</span></p><p style="line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="font-size:18px"></span></p><p style="line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="line-height:1.6em"><span style="font-size:18px">5、重启eclipse或myeclipse</span></span></p><p style="line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="line-height:1.6em"><span style="font-size:18px"></span></span></p><img src="https://img-blog.csdn.net/20141214215544265?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXVhbmxhaXNoaW5pMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><p style="color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="font-size:18px"></span></p><p style="line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="font-size:18px"></span></p><p style="line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="font-size:18px">如图:已经可以在<strong>outline中看见对应的方法了</strong></span></p><span style="font-size:18px"><wbr style="line-height:28px; font-family:'Hiragino Sans GB W3','Hiragino Sans GB',Arial,Helvetica,simsun,u5b8bu4f53; background-color:rgb(249,247,241)"><span style="line-height:22px; font-family:'AR PL UKai CN'">还使用javap命令查看DataObject类编译后的class文件,看看是否生成了相应的getter、setter方法</span></wbr></span><p style="line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"></p><h2 style="color:rgb(106,62,9)"><span style="line-height:19.200000762939453px; font-family:Georgia; color:red"><span style="font-size:18px">二.eclipse / myeclipse 手动安装 lombok</span></span></h2><span style="font-size:18px; line-height:19.200000762939453px; font-family:Georgia"><span style="color:#6a3e09"> </span>   1. 将 lombok.jar 复制到 myeclipse.ini / eclipse.ini 所在的文件夹目录下</span><p></p><p style="line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="font-size:18px; line-height:19.200000762939453px; font-family:Georgia">    2. 打开 eclipse.ini / myeclipse.ini,在最后面插入以下两行并保存:        -Xbootclasspath/a:lombok.jar        -javaagent:lombok.jar</span></p><p style="line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="font-size:18px; line-height:19.200000762939453px; font-family:Georgia">    3.重启 eclipse / myeclipse</span></p><p style="font-size:16px; color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"></p><p style="color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"></p><h1><span style="line-height:19.200000762939453px; font-family:Georgia; color:red"><span style="font-size:24px">lombok 注解使用</span></span></h1><span style="line-height:19.200000762939453px; font-family:Georgia"><span style="font-size:18px">    lombok 提供的注解不多,可以参考官方视频的讲解和官方文档。    <span style="color:rgb(0,128,0)">Lombok 注解在线帮助文档:</span><a target=_blank target="_blank" href="http://projectlombok.org/features/index" style="color:rgb(29,88,209); text-decoration:none">http://projectlombok.org/features/index.</a>    <span style="color:rgb(153,51,0)">下面介绍几个我常用的 lombok 注解:</span>        <span style="color:rgb(0,0,255)">@Data   </span>:注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法        <span style="color:rgb(0,0,255)">@Setter</span>:注解在属性上;为属性提供 setting 方法        <span style="color:rgb(0,0,255)">@Getter</span>:注解在属性上;为属性提供 getting 方法        <span style="color:rgb(0,0,255)">@Log4j </span>:注解在类上;为类提供一个 属性名为log 的 log4j 日志对象        <span style="color:rgb(0,0,255)">@NoArgsConstructor</span>:注解在类上;为类提供一个无参的构造方法        <span style="color:rgb(0,0,255)">@AllArgsConstructor</span>:注解在类上;为类提供一个全参的构造方法</span></span><p></p><p style="color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="line-height:19.200000762939453px; font-family:Georgia"><span style="font-size:18px"></span></span></p><p style="color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="line-height:19.200000762939453px; font-family:Georgia"><span style="font-size:18px"><span style="font-family:Georgia; line-height:19.200000762939453px">下面是简单示例</span><br style="font-family:Georgia; line-height:19.200000762939453px" /><span style="font-family:Georgia; line-height:19.200000762939453px; color:rgb(153,51,0)">    1.不使用 lombok 的方案</span></span></span></p><p style="color:rgb(106,62,9); line-height:22px; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; border:none; list-style:none outside; width:600px; overflow:hidden; clear:both; font-family:'AR PL UKai CN'"><span style="line-height:19.200000762939453px; font-family:Georgia"><span style="font-family: Georgia; line-height: 19.200000762939453px; color: rgb(153, 51, 0);"></span></span></p><pre name="code" class="java" style="font-size: 18px;">public class Person {

    private String id;
    private String name;
    private String identity;
    private Logger log = Logger.getLogger(Person.class);
    
    public Person() {
        
    }
    
    public Person(String id, String name, String identity) {
        this.id              = id;
        this.name       = name;
        this.identity  = identity;
    }
    
    public String getId() {
        return id;
    }
    
    public String getName() {
        return name;
    }
    
    public String getIdentity() {
        return identity;
    }
    
   public void setId(String id) {
        this.id = id;
    }
    
    public void setName(String name) {
        this.name = name;
   }
    
    public void setIdentity(String identity) {
        this.identity = identity;
    }
}

2.使用 lombok 的方案

<span style="font-size:18px;">@Data
@Log4j
@NoArgsConstructor
@AllArgsConstructor
public class Person {

    private String id;
    private String name;
  private String identity;
    
}</span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值