柏竹 柏竹
首页
后端
前端
  • 应用推荐
关于
友链
  • 分类
  • 标签
  • 归档

柏竹

奋斗柏竹
首页
后端
前端
  • 应用推荐
关于
友链
  • 分类
  • 标签
  • 归档
  • Java基础

  • JavaWeb

  • 拓展技术

    • Java设计模式
    • Java序列化
    • Stream流
    • Lombok简化开发应用
      • 实例
        • val
        • @NonNull
        • @Cleanup
        • @ToString
        • @EqualsAndHashCode
        • 构造方法
        • @Value
    • JavaZip解压缩
    • Lua应用
    • OpenResty
    • C++快速上手
    • PHP快速上手
    • SpEL表达式
    • velocity模板引擎
    • Flyway数据库版本管理工具
    • 企业项目实战问题
  • 框架技术

  • 数据库

  • 数据结构

  • Spring

  • SpringMVC

  • SpringBoot

  • SpringClound

  • Ruoyi-Vue-Plus

  • 后端
  • 拓展技术
柏竹
2021-10-08
目录

Lombok简化开发应用

# Lombok简化开发应用

Lombok是一个Java库,它是款可帮助开发人员减少 Java对象(POJO)的代码冗余,通过注解实现这一目的

常用注解说明:

注解 用于 说明
val 属性声明 将变量声明为 final
@NonNull 方法参数、属性 对参数进行是否为空的校验,空则抛出NPE异常
@Cleanup 局部变量 当前变量范围内即将执行完毕退出之前会自动清理资源,自动生成try-finally这样的代码来关闭流
@Getter/@Setter 属性、类 无需手写get、set方法
@ToString 类 toString方法 可设置 排除属性、包含类型
@EqualsAndHashCode 类 自动生成 equals方法和hashCode方法
@NoArgsConstructor 类 生成无参构造方法
@RequiredArgsConstructor 类 可指定字段进生成构造方法(静态工厂方法
@AllArgsConstructor 类 全参数构造方法
@Data 类 同时使用了@ToString、@Equals、@HashCode、@Getter、@Setter、@RequiredArgsConstrutor注解
@Value 类 属性添加final声明,只提供get

# 实例

安装插件 Lombok(IDEA默认捆绑该插件)

添加依赖 pom.xml

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <scope>provided</scope>
</dependency>

# val

public static void main(String[] args) {
    val sets = new HashSet<String>();
    val lists = new ArrayList<String>();
    val maps = new HashMap<String, String>();
    //=>相当于如下
    final Set<String> sets2 = new HashSet<>();
    final List<String> lists2 = new ArrayList<>();
    final Map<String, String> maps2 = new HashMap<>();
}

# @NonNull

public void notNullExample(@NonNull String string) {
    string.length();
}
//=>相当于
public void notNullExample(String string) {
    if (string != null) {
        string.length();
    } else {
        throw new NullPointerException("null");
    }
}

# @Cleanup

public static void main(String[] args) {
    try {
        @Cleanup InputStream inputStream = new FileInputStream(args[0]);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    //=>相当于
    InputStream inputStream = null;
    try {
        inputStream = new FileInputStream(args[0]);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

# @ToString

@ToString(exclude = "id", callSuper = true, includeFieldNames = true)
public class LombokDemo {
    private int id;
    private String name;
    private int age;
    public static void main(String[] args) {
        //输出LombokDemo(super=LombokDemo@48524010, name=null, age=0)
        System.out.println(new LombokDemo());
    }
}

# @EqualsAndHashCode

@EqualsAndHashCode(exclude = {"id", "shape"}, callSuper = false)
public class LombokDemo {
    private int id;
    private String shap;
}

# 构造方法

@NoArgsConstructor
@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor
public class LombokDemo {
    @NonNull
    private int id;
    @NonNull
    private String shap;
    private int age;
    public static void main(String[] args) {
        new LombokDemo(1, "circle");
        //使用静态工厂方法
        LombokDemo.of(2, "circle");
        //无参构造
        new LombokDemo();
        //包含所有参数
        new LombokDemo(1, "circle", 2);
    }
}

# @Value

@Value
public class LombokDemo {
    @NonNull
    private int id;
    @NonNull
    private String shap;
    private int age;
    //相当于 (只有get方法
    private final int id;
    public int getId() {
        return this.id;
    }
    ...
}

参考:https://segmentfault.com/a/1190000020181422 (opens new window)

#Java技巧

← Stream流 JavaZip解压缩→

最近更新
01
HTTPS自动续签
10-21
02
博客搭建-简化版(脚本)
10-20
03
ruoyi-vue-plus-部署篇
07-13
更多文章>
Theme by Vdoing | Copyright © 2019-2024 | 桂ICP备2022009417号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式