Lombok介绍和应用

1、Lombok是什么
    Lombok是一个可通过简单的注解简化消除一些必须但显得臃肿的java代码的工具。比如新建一个类,通常需要手动建立getter和setter方法,构造函数等,Lombok的作用就是省去创建这些代码的麻烦,在编译代码时自动生成这些方法。
    虽然有的ID工具自带生成这些方法的功能,但使用Lombok会使代码更简洁,使用更方便。
2、Lombok安装
2.1、网络资源

    Lombok的官方地址:https://projectlombok.org/
    Lombok的Github地址:https://github.com/rzwitserloot/lombok
2.2、安装
    下载jar包,安装,并添加maven依赖
<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.10</version>
    </dependency>
</dependencies>
3、应用示例
3.1、避免空指针

使用lombok:
import lombok.NonNull;
    public class NonNullExample extends Something {
        private String name;  
        public NonNullExample(@NonNull Person person) {
        super("Hello");
        this.name = person.getName();
    }
}
不使用lombok:
public class NonNullExample extends Something {
    private String name;  
    public NonNullExample(@NonNull Person person) {
        super("Hello");
        if (person == null) {
            throw new NullPointerException("person");
        }
        this.name = person.getName();
    }
}
3.2、文件流处理
使用lombok:
import lombok.Cleanup;
import java.io.*;
public class CleanupExample {
    public static void main(String[] args) throws IOException {
        @Cleanup InputStream in = new FileInputStream(args[0]);
        @Cleanup OutputStream out = new FileOutputStream(args[1]);
        byte[] b = new byte[1024];
        while (true) {
            int r = in.read(b);
            if (r != -1) 
               out.write(b, 0, r);
        }
    }
}
不使用lombok:
import java.io.*;
    public class CleanupExample {
        public static void main(String[] args) throws IOException {
            InputStream in = new FileInputStream(args[0]);
            try {
                OutputStream out = new FileOutputStream(args[1]);
                try {
                    byte[] b = new byte[1024];
                    while (true) {
                    int r = in.read(b);
                    if (r != -1) 
                       out.write(b, 0, r);
                    }
                } finally {
                    if (out != null) {
                        out.close();
                    }
                }
            } finally {
                if (in != null) {
                in.close();
            }
        }
    }
}
3.3、自动生成Getter/Setter方法
使用lombok:
    import lombok.AccessLevel;
    import lombok.Getter;
    import lombok.Setter;
    public class GetterSetterExample {
        @Getter @Setter private int age = 10;
        @Setter(AccessLevel.PROTECTED) private String name;
    }
不使用lombok:
public class GetterSetterExample {
    private int age = 10;
    private String name;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    protected void setName(String name) {
        this.name = name;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大浪淘沙胡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值