lombok安装及使用

15 篇文章 0 订阅
1 篇文章 0 订阅


lombok官网: https://projectlombok.org/

参考:https://www.cnblogs.com/mr-wuxiansheng/p/7563364.html

1.eclipse安装lombok

-javaagent:lombok.jar
-Xbootclasspath/a:lombok.jar
  • 打开eclipse,project–clean–
    在这里插入图片描述

普通项目使用lombok

将这个lombok的jar包复制到lib中导入使用

maven项目使用lombok

在pom中加入

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

2.idea安装lombok

1.安装插件:
(idea有插件不用下载jar…)
file—setting—plugins—Browse repositories…—搜索lombok—点击install(图片上是已经安装了)
在这里插入图片描述
2.添加lombok的maven的pom.xml依赖:

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

3.lombok使用

注解:

@Data   :注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法
@Setter:注解在属性上;为属性提供 setting 方法
@Getter:注解在属性上;为属性提供 getting 方法
@Value :Getter、toString()equals()hashCode()、一个全参的构造方法
@Builder:Builder内部类和全字段的构造器,没有Getter、Setter、toString()@ToStringtoString()方法
@Cleanup:该注解的对象,如Stream对象,如果有close()方法,那么在该对象作用域离开时会自动关闭。
@Log:一组日志相关注解,标注的类会隐式的定一个了一个名为log的日志对象。
@Log4j :注解在类上;为类提供一个 属性名为log 的 log4j 日志对象
@Log4j2@Slf4j@XSlf4j
    @Log  
    public  class User {  
        public  static  void  main(String[] args) {
            System.out.println(log.getClass()); 
            log.info("app log."); 
        }
    }
@NoArgsConstructor:注解在类上;为类提供一个无参的构造方法
@AllArgsConstructor:注解在类上;为类提供一个全参的构造方法
@RequiredArgsConstructor: 增加必选参数构造器

构造器:
可以使用access属性定制访问级别,如:”access = AccessLevel.PROTECTED”
前两个比较简单,必选参数构造器需要配合 @NonNull 注解使用,只有标记了 @NonNull 注解的字段才会被纳入 RequiredArgsConstructor 构造器中。

@RequiredArgsConstructor
public class User {
    @NonNull
    private Integer id;
    @NonNull
    private String userName;
    @NonNull
    private String password;
    private String email;
    private Integer age;
    private Date signupTime;

    public static void main(String[] args) {
        /*
         * User user=new User();
         * User user = new User(1001, "pollyduan", "123456", "pollyduan@pollyduan.com", 30, new Date());
         * //The constructor User() is undefined
         */
        User user=new User(1001, "pollyduan", "123456");//ok
        System.out.println(user);
    }
}

属性注解

@Getter(lazy=true) 懒加载
是对象初始化时,该字段并不会真正的初始化;而是第一次访问该字段时才进行初始化字段的操作。

package com.pollyduan;

import lombok.Data;
import lombok.Getter;

@Data
public class GetterLazyExample {
    @Getter(lazy = true)
    private final int[] cached = expensive();
    private Integer id;

    private int[] expensive() {
        int[] result = new int[100];
        for (int i = 0; i < result.length; i++) {
            result[i] = i;
            System.out.println(i);
        }
        System.out.println("cached 初始化完成。");
        return result;
    }
    public static void main(String[] args) {
        GetterLazyExample obj=new GetterLazyExample();
        obj.setId(1001);
        System.out.println("打印id:"+obj.getId());
        System.out.println("cached 还没有初始化哟。");
        // obj.getCached();
    }
}
打印id:1001
cached 还没有初始化哟。
打开obj.getCached();的注释,获取这个字段的值,你就会发现它真的初始化了。
打印id:1001 cached 还没有初始化哟。 0  1  ...  97  98  99 cached 初始化完成。

@SneakyThrows 隐藏异常
自动捕获检查异常。

我们知道,java对于检查异常,需要在编码时进行捕获,或者throws抛出。
该注解的作用是将检查异常包装为运行时异常,那么编码时就无需处理异常了。
提示:不过这并不是友好的编码方式,因为你编写的api的使用者,不能显式的获知需要处理检查异常。

import lombok.SneakyThrows;

public class SneakyThrowsExample {
    @SneakyThrows({UnsupportedEncodingException.class})
    public void test(byte[] bytes) {
        String str = new String(bytes, "UTF8");
    }
    @SneakyThrows({UnsupportedEncodingException.class,FileNotFoundException.class})
    public void test2(byte[] bytes) {
        FileInputStream file=new FileInputStream("no_texists.txt");
        String str=new String(bytes, "UTF8");
    }
    @SneakyThrows
    public void test3(byte[] bytes) {
        FileInputStream file=new FileInputStream("no_texists.txt");
        String str=new String(bytes, "UTF8");
    }

}

注解接受一个class数组的value属性,如果未指定value属性,默认捕获所有异常。
相当于

package com.pollyduan;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;

import lombok.SneakyThrows;

public class SneakyThrowsExample {
    public void test(byte[] bytes) {
        try {
            String str = new String(bytes, "UTF8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    public void test2(byte[] bytes) {
        try {
            FileInputStream file=new FileInputStream("no_texists.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            String str=new String(bytes, "UTF8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    public void test3(byte[] bytes) {
        try {
            FileInputStream file=new FileInputStream("no_texists.txt");
            String str=new String(bytes, "UTF8");
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

}

辅助注解

@NonNull :
标记在字段上,表示非空字段。
标注在方法参数上,会在第一次使用该参数是判断是否为空。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值