Lombok 安装和使用小技巧

目录
  • 简介
  • 安装 Lombok
  • Lombok 使用
    • 具体使用
    • 常用注解
  • 总结:

简介

为了减少写一些 get/set/toString 方法,让项目代码更加整洁,提高开发效率,发现大家都开始采用 Lombok 这个工具。Lombok 是一个 Java 类库,它会自动插入编辑器和构建工具,用于帮助开发人员消除 Java 中冗长样板代码。而我们开发人员所要做的,仅仅是添加几个 Lombok 中的注解,就可以替换掉原来的多行 get/set/toString 方法代码,既简洁也易于维护。下面我们就来看看,如何安装并使用这一工具。

安装 Lombok

日常开发中,相信大多数人现在使用的都是 IDEA 这个 Java 神器了,如果你还在使用 Eclipse 或者 MyEclipse 等工具,那强烈推荐你去体验一把 IDEA,相信你一用上它就会爱上他它的强大!下面我就一在 IDEA 中使用 Lombok 为例,看看如何安装并使用它。

在先前 IDEA 的版本中,Lombok 是需要通过插件来安装的,安装方法如下:依次进入File -> Settings -> Plugins,然后搜索 Lombok ,最后进行安装即可。而在新版本的 IDEA 中,Lombok 已经被集成到 IDEA 中,我们不用再去安装它就可以直接使用,可以说是十分方便了。

老版本 IDEA 安装 Lombok

新版本中集成了 Lombok

以上就是 Lombok 的安装过程了,是不是十分简单?那接下来我们就来看看,如何在我们的项目中使用 Lombok!

Lombok 使用

现在大家进行项目管理时用的工具大多应该都是 Maven,所以我们直接在需要使用 Lombok 的项目中加入 Lombok 编译支持,也就是在 pom.xml 文件中加入以下依赖。

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

导入相关依赖之后,接下来就是具体使用过程了。

具体使用

在需要的实体类中引入相关注解即可,只不过注解不同它们所对应的功能也不同,而且同一个注解可能在不同位置的功能也不一样。如下图;

常用注解

@Data

注解在 类 上:给类的所有属性提供 get 和 set 方法,此外还有 equals、canEqual、hashCode、toString 方法以及 默认参数为空的构造方法;

使用前:

package com.cunyu.user.entity;

public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;

    public User() {
    }

    public Long getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public Integer getAge() {
        return this.age;
    }

    public String getEmail() {
        return this.email;
    }

    public void setId(final Long id) {
        this.id = id;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public void setAge(final Integer age) {
        this.age = age;
    }

    public void setEmail(final String email) {
        this.email = email;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof User)) {
            return false;
        } else {
            User other = (User)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label59: {
                    Object this$id = this.getId();
                    Object other$id = other.getId();
                    if (this$id == null) {
                        if (other$id == null) {
                            break label59;
                        }
                    } else if (this$id.equals(other$id)) {
                        break label59;
                    }

                    return false;
                }

                Object this$age = this.getAge();
                Object other$age = other.getAge();
                if (this$age == null) {
                    if (other$age != null) {
                        return false;
                    }
                } else if (!this$age.equals(other$age)) {
                    return false;
                }

                Object this$name = this.getName();
                Object other$name = other.getName();
                if (this$name == null) {
                    if (other$name != null) {
                        return false;
                    }
                } else if (!this$name.equals(other$name)) {
                    return false;
                }

                Object this$email = this.getEmail();
                Object other$email = other.getEmail();
                if (this$email == null) {
                    if (other$email != null) {
                        return false;
                    }
                } else if (!this$email.equals(other$email)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof User;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $id = this.getId();
        int result = result * 59 + ($id == null ? 43 : $id.hashCode());
        Object $age = this.getAge();
        result = result * 59 + ($age == null ? 43 : $age.hashCode());
        Object $name = this.getName();
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        Object $email = this.getEmail();
        result = result * 59 + ($email == null ? 43 : $email.hashCode());
        return result;
    }

    public String toString() {
        Long var10000 = this.getId();
        return "User(id=" + var10000 + ", name=" + this.getName() + ", age=" + this.getAge() + ", email=" + this.getEmail() + ")";
    }
}

使用后:

package com.cunyu.user.entity;
import lombok.Data;
/**
 * Created with IntelliJ IDEA.
 *
 * @author : zhangliang
 * @version : 1.0
 * @project : User
 * @package : com.cunyu.user.entity
 * @className : User
 * @createTime : 2021/8/6 17:14
 * @description : 用户实体类
 */

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

@Setter

注解在 类 上:为该类所有属性均提供 set 方法,同时提供 默认构造方法;

使用前:

package com.cunyu.user.entity;
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
    public User() {
    }
    public void setId(final Long id) {
        this.id = id;
    }
    public void setName(final String name) {
        this.name = name;
    }
    public void setAge(final Integer age) {
        this.age = age;
    }
    public void setEmail(final String email) {
        this.email = email;
    }
}

使用后:

package com.cunyu.user.entity;

import lombok.Setter;
/**
 * Created with IntelliJ IDEA.
 *
 * @author : zhangliang
 * @version : 1.0
 * @project : User
 * @package : com.cunyu.user.entity
 * @className : User
 * @createTime : 2021/8/6 17:14
 * @description : 用户实体类
 */
@Setter
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

注解在 属性 上:为该属性提供 set 方法,同时提供 默认构造方法;

使用前:

package com.cunyu.user.entity;
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
    public User() {
    }
    public void setId(final Long id) {
        this.id = id;
    }
}

使用后:

package com.cunyu.user.entity;
import lombok.Setter;
/**
 * Created with IntelliJ IDEA.
 *
 * @author : zhangliang
 * @version : 1.0
 * @project : User
 * @package : com.cunyu.user.entity
 * @className : User
 * @createTime : 2021/8/6 17:14
 * @description : 用户实体类
 */
public class User {
    @Setter
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

@Getter

注解在 类 上:为该类所有属性均提供 get 方法,同时提供 默认构造方法;

使用前:

package com.cunyu.user.entity;
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
    public User() {
    }
    public Long getId() {
        return this.id;
    }
    public String getName() {
        return this.name;
    }
    public Integer getAge() {
        return this.age;
    }
    public String getEmail() {
        return this.email;
    }
}

使用后:

package com.cunyu.user.entity;
import lombok.Getter;
/**
 * Created with IntelliJ IDEA.
 *
 * @author : zhangliang
 * @version : 1.0
 * @project : User
 * @package : com.cunyu.user.entity
 * @className : User
 * @createTime : 2021/8/6 17:14
 * @description : 用户实体类
 */
@Getter
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

注解在 属性 上:为该属性提供 get 方法,同时提供 默认构造方法;

使用前:

package com.cunyu.user.entity;
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
    public User() {
    }
    public Long getId() {
        return this.id;
    }
}

使用后:

package com.cunyu.user.entity;
import lombok.Getter;
/**
 * Created with IntelliJ IDEA.
 *
 * @author : zhangliang
 * @version : 1.0
 * @project : User
 * @package : com.cunyu.user.entity
 * @className : User
 * @createTime : 2021/8/6 17:14
 * @description : 用户实体类
 */
public class User {
    @Getter
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

@ToString

注解在 类 上:生成所有参数的 toString() 方法,同时提供 默认构造方法;

使用前:

package com.cunyu.user.entity;
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
    public User() {
    }
    public String toString() {
        return "User(id=" + this.id + ", name=" + this.name + ", age=" + this.age + ", email=" + this.email + ")";
    }
}

使用后:

package com.cunyu.user.entity;
import lombok.ToString;
/**
 * Created with IntelliJ IDEA.
 *
 * @author : zhangliang
 * @version : 1.0
 * @project : User
 * @package : com.cunyu.user.entity
 * @className : User
 * @createTime : 2021/8/6 17:14
 * @description : 用户实体类
 */
@ToString
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

@Value

注解在 类 上:生成 get 方法,以及 equals、hashCode、toString 方法,同时提供 含所有参数的构造方法;

使用前:

package com.cunyu.user.entity;
public final class User {
    private final Long id;
    private final String name;
    private final Integer age;
    private final String email;
    public User(final Long id, final String name, final Integer age, final String email) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.email = email;
    }
    public Long getId() {
        return this.id;
    }
    public String getName() {
        return this.name;
    }
    public Integer getAge() {
        return this.age;
    }
    public String getEmail() {
        return this.email;
    }
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof User)) {
            return false;
        } else {
            User other;
            label56: {
                other = (User)o;
                Object this$id = this.getId();
                Object other$id = other.getId();
                if (this$id == null) {
                    if (other$id == null) {
                        break label56;
                    }
                } else if (this$id.equals(other$id)) {
                    break label56;
                }
                return false;
            }
            label49: {
                Object this$age = this.getAge();
                Object other$age = other.getAge();
                if (this$age == null) {
                    if (other$age == null) {
                        break label49;
                    }
                } else if (this$age.equals(other$age)) {
                    break label49;
                }

                return false;
            }
            Object this$name = this.getName();
            Object other$name = other.getName();
            if (this$name == null) {
                if (other$name != null) {
                    return false;
                }
            } else if (!this$name.equals(other$name)) {
                return false;
            }

            Object this$email = this.getEmail();
            Object other$email = other.getEmail();
            if (this$email == null) {
                if (other$email != null) {
                    return false;
                }
            } else if (!this$email.equals(other$email)) {
                return false;
            }

            return true;
        }
    }
    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $id = this.getId();
        int result = result * 59 + ($id == null ? 43 : $id.hashCode());
        Object $age = this.getAge();
        result = result * 59 + ($age == null ? 43 : $age.hashCode());
        Object $name = this.getName();
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        Object $email = this.getEmail();
        result = result * 59 + ($email == null ? 43 : $email.hashCode());
        return result;
    }
    public String toString() {
        Long var10000 = this.getId();
        return "User(id=" + var10000 + ", name=" + this.getName() + ", age=" + this.getAge() + ", email=" + this.getEmail() + ")";
    }
}

使用后:

package com.cunyu.user.entity;
import lombok.Value;
/**
 * Created with IntelliJ IDEA.
 *
 * @author : zhangliang
 * @version : 1.0
 * @project : User
 * @package : com.cunyu.user.entity
 * @className : User
 * @createTime : 2021/8/6 17:14
 * @description : 用户实体类
 */

@Value
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

@AllArgsConstructor

注解在 类 上:为类提供一个 全参构造方法,但此时不再提供默认构造方法;

使用前:

package com.cunyu.user.entity;
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;

    public User(final Long id, final String name, final Integer age, final String email) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.email = email;
    }
}

使用后:

package com.cunyu.user.entity;

import lombok.AllArgsConstructor;
/**
 * Created with IntelliJ IDEA.
 *
 * @author : zhangliang
 * @version : 1.0
 * @project : User
 * @package : com.cunyu.user.entity
 * @className : User
 * @createTime : 2021/8/6 17:14
 * @description : 用户实体类
 */
@AllArgsConstructor
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

@NoArgsConstructor

注解在 类 上:为类提供一个 无参构造方法;

使用前:

package com.cunyu.user.entity;
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
    public User() {
    }
}

使用后:

package com.cunyu.user.entity;
import lombok.NoArgsConstructor;
/**
 * Created with IntelliJ IDEA.
 *
 * @author : zhangliang
 * @version : 1.0
 * @project : User
 * @package : com.cunyu.user.entity
 * @className : User
 * @createTime : 2021/8/6 17:14
 * @description : 用户实体类
 */
@NoArgsConstructor
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

@RequiredArgsConstructor

注解在 类 上:使用类中所有带 @NonNull 注解的或带有 final 修饰的成员变量生成对应构造方法;

使用前:

package com.cunyu.user.entity;
import lombok.NonNull;
public class User {
    @NonNull
    private Long id;
    private String name;
    private Integer age;
    @NonNull
    private String email;

    public User(@NonNull final Long id, @NonNull final String email) {
        if (id == null) {
            throw new NullPointerException("id is marked non-null but is null");
        } else if (email == null) {
            throw new NullPointerException("email is marked non-null but is null");
        } else {
            this.id = id;
            this.email = email;
        }
    }
}

使用后:

package com.cunyu.user.entity;
import lombok.RequiredArgsConstructor;
/**
 * Created with IntelliJ IDEA.
 *
 * @author : zhangliang
 * @version : 1.0
 * @project : User
 * @package : com.cunyu.user.entity
 * @className : User
 * @createTime : 2021/8/6 17:14
 * @description : 用户实体类
 */
@RequiredArgsConstructor
public class User {
    @NonNull
    private Long id;
    private String name;
    private Integer age;
    @NonNull
    private String email;
}

@NonNull

注解在 属性 上,自动生成一个关于该参数的非空检查,若参数为 null,则抛出一个空指针异常,同时提供 默认构造方法,具体用法可以参照上面的例子;

@EqualsAndHashCode

注解在 类 上,生成 equals、canEquals、hasnCode 方法,同时会生成默认构造方法;

使用前:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.cunyu.user.entity;
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;

    public User() {
    }
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof User)) {
            return false;
        } else {
            User other = (User)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label59: {
                    Object this$id = this.id;
                    Object other$id = other.id;
                    if (this$id == null) {
                        if (other$id == null) {
                            break label59;
                        }
                    } else if (this$id.equals(other$id)) {
                        break label59;
                    }

                    return false;
                }

                Object this$age = this.age;
                Object other$age = other.age;
                if (this$age == null) {
                    if (other$age != null) {
                        return false;
                    }
                } else if (!this$age.equals(other$age)) {
                    return false;
                }

                Object this$name = this.name;
                Object other$name = other.name;
                if (this$name == null) {
                    if (other$name != null) {
                        return false;
                    }
                } else if (!this$name.equals(other$name)) {
                    return false;
                }

                Object this$email = this.email;
                Object other$email = other.email;
                if (this$email == null) {
                    if (other$email != null) {
                        return false;
                    }
                } else if (!this$email.equals(other$email)) {
                    return false;
                }

                return true;
            }
        }
    }
    protected boolean canEqual(final Object other) {
        return other instanceof User;
    }
    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $id = this.id;
        int result = result * 59 + ($id == null ? 43 : $id.hashCode());
        Object $age = this.age;
        result = result * 59 + ($age == null ? 43 : $age.hashCode());
        Object $name = this.name;
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        Object $email = this.email;
        result = result * 59 + ($email == null ? 43 : $email.hashCode());
        return result;
    }
}

使用后:

package com.cunyu.user.entity;
import lombok.EqualsAndHashCode;
/**
 * Created with IntelliJ IDEA.
 *
 * @author : zhangliang
 * @version : 1.0
 * @project : User
 * @package : com.cunyu.user.entity
 * @className : User
 * @createTime : 2021/8/6 17:14
 * @description : 用户实体类
 */

@EqualsAndHashCode
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

@Cleanup

注解在 局部变量 前,保证该变量代表的资源使用后自动关闭,默认调用资源的 close() 方法,若该资源有其它关闭方法,可用 @Cleanup("方法名") 来指定要调用的方法,同时提供 默认构造方法;

使用前:

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[10000];
                while (true) {
                    int r = in.read(b);
                    if (r == -1) break;
                    out.write(b, 0, r);
                }
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
}

使用后:

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[10000];
        while (true) {
            int r = in.read(b);
            if (r == -1) break;
            out.write(b, 0, r);
        }
    }
}

@Synchronized

注解在 类方法 或 实例方法:效果与 synchronized 关键字相同,区别在于锁对象不同,对于类方法和实例方法,synchronized 关键字的锁对象分别是 类的 class 对象和 this 对象,而 @Synchronized 的锁对象分别是 私有静态 final 对象 lock 和 私有 final 对象 lock,也可以自己指定锁对象,同时提供默认构造方法;

使用前:

public class SynchronizedExample {
    private static final Object $LOCK = new Object[0];
    private final Object $lock = new Object[0];
    private final Object readLock = new Object();
    public static void hello() {
        synchronized($LOCK) {
            System.out.println("world");
        }
    }
    public int answerToLife() {
        synchronized($lock) {
            return 42;
        }
    }

    public void foo() {
        synchronized(readLock) {
            System.out.println("bar");
        }
    }
}

使用后:

import lombok.Synchronized;
public class SynchronizedExample {
    private final Object readLock = new Object();
    @Synchronized
    public static void hello() {
        System.out.println("world");
    }
    @Synchronized
    public int answerToLife() {
        return 42;
    }
    @Synchronized("readLock")
    public void foo() {
        System.out.println("bar");
    }
}

@SneakyThrows

注解在 方法 上:将方法中的代码用 try-catch 语句包裹,捕获异常并在 catch 中用 Lombok.sneakyThrow(e) 将异常抛出,还可以用 @SneakyThrows(Exception.class) 的形式指定抛出异常类型,同时提供 默认构造方法;

使用前:

import lombok.Lombok;
public class SneakyThrowsExample implements Runnable {
    public String utf8ToString(byte[] bytes) {
        try {
            return new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw Lombok.sneakyThrow(e);
        }
    }
    public void run() {
        try {
            throw new Throwable();
        } catch (Throwable t) {
            throw Lombok.sneakyThrow(t);
        }
    }
}

使用后:

import lombok.SneakyThrows;
public class SneakyThrowsExample implements Runnable {
    @SneakyThrows(UnsupportedEncodingException.class)
    public String utf8ToString(byte[] bytes) {
        return new String(bytes, "UTF-8");
    }
    @SneakyThrows
    public void run() {
        throw new Throwable();
    }
}

@Log

注解在 类 上:主要用于我们记录日志信息,同时提供 默认构造方法。它封装了多个主流 Log 库,主要有如下几个;

  • @Log
  • @Slf4j
  • Log4j
  • Log4j2

总结:

到此这篇关于Lombok 安装和使用小技巧的文章就介绍到这了,更多相关Lombok 内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Java开发神器Lombok安装与使用详解

    目录 安装 Idea插件的安装 maven中pom文件的导入 使用 val @Data @Getter/@Setter @Log4j @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode @NonNull @RequiredArgsConstructor @Cleanup @ToString @Value @SneakyThrows @Synchronized @Builder 安装 Lombok的安装分两部分:Idea插件的安装和m

  • 详解Lombok快速上手(安装、使用与注解参数)

    Lombok插件安装与使用说明 在实习中发现项目中IDE一直报检查错误,原来是使用了Lombok注解的黑科技,这里整理了一些日常编码中能遇到的所有关于它的使用详解,其实lombok项目的产生就是为了省去我们手动创建getter和setter方法等等一些基本组件代码的麻烦,它能够在我们编译源码的时候自动帮我们生成getter和setter方法.即它最终能够达到的效果是:在源码中没有getter和setter等组件方法,但是在编译生成的字节码文件中有getter和setter等组件方法. 常见参数

  • Lombok插件的安装与简单使用步骤

    Lombok插件的安装与简单使用 关于Lombok:可以给类的属性生成set get,构造方法,toString方法等 Lombok插件会使我们的代码书写变得更加方便 使用它我们需要以下几步 1.添加依赖: <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> 2.在idea中进行安装 File-&

  • IDEA中安装和使用Lombok插件的方法

    背景 我们在开发过程中,通常都会定义大量的JavaBean,然后通过IDE去生成其属性的构造器.getter.setter.equals.hashcode.toString方法,当要对某个属性进行改变时,比如命名.类型等,都需要重新去生成上面提到的这些方法,那Java中有没有一种方式能够避免这种重复的劳动呢?答案是有,我们来看一下下面这张图,右面是一个简单的JavaBean,只定义了两个属性,在类上加上了@Data,从左面的结构图上可以看到,已经自动生成了上面提到的方法. Lombok简介 om

  • Lombok插件安装(IDEA)及配置jar包使用详解

    点击进入Lombok官网下载Lombok jar包 使用Lombok可能需要注意的地方 (1).当你的IDE是Idea时,要注意你的Idea是支持Lombok的,如果不支持请更换高版本尝试(这里采用2018 3.3). (2).在使用Lombok时,你的编辑器可能会报错,这时请在你的IDE中安装Lombok插件(如果使用的Idea则直接搜索Lombok插件,选择星级最高的,直接安装就是,其他Ide类同). (3).参数的处理往往都是根据项目需求来进行,请妥善处理参数. (4).如果你无法访问Lo

  • Intellij IDEA安装lombok插件及使用详解

    项目中经常使用bean,entity等类,绝大部分数据类类中都需要get.set.toString.equals和hashCode方法,虽然eclipse和idea开发环境下都有自动生成的快捷方式,但自动生成这些代码后,如果bean中的属性一旦有修改.删除或增加时,需要重新生成或删除get/set等方法,给代码维护增加负担.而使用了lombok则不一样,使用了lombok的注解(@Setter,@Getter,@ToString,@@RequiredArgsConstructor,@Equals

  • 解决在Idea 2020.2下使用 Lombok的注解不生效的问题(插件安装了,依赖也写了,自动注解也设置了)

    如题必要条件都有但还是不能生效 虽然不是灰色,但是还是可以看到上面的导包是灰色的,并且没有任何报错. 而且可以看到上面正常正确的类是有图标的,但是Test这个类没有.在其他类里面也是不能引用get/set方法的,在一顿百度和折腾之后还是没有找到正确的解决方法,无奈之下重启后发现问题解决. 正常情况下不应该出现这种问题,连图标都没有,可能是Idea 2020.2版本的BUG.,反正我是除了重启没有找到解决办法. 下载这个版本的lombok就OK了,我没有经过任何修改,可以放心下载,如果不放心的可以

  • Spring Boot中lombok的安装与使用详解

    前言 众所周知Spring Boot是非常高效的开发框架,lombok是一套代码模板解决方案,将极大提升开发的效率,这里介绍给大家使用.文中详细介绍了lombok的安装与使用教程,话不多说了,来一起看看详细的介绍吧. 1. Lombok Lombok想要解决了的是在我们实体Bean中大量的Getter/Setter方法,以及toString, hashCode等可能不会用到,但是某些时候仍然需要复写,以期方便使用的方法:在使用Lombok之后,将由其来自动帮你实现代码生成,注意,其是在运行过程中

  • Lombok 安装和使用小技巧

    目录 简介 安装 Lombok Lombok 使用 具体使用 常用注解 总结: 简介 为了减少写一些 get/set/toString 方法,让项目代码更加整洁,提高开发效率,发现大家都开始采用 Lombok 这个工具.Lombok 是一个 Java 类库,它会自动插入编辑器和构建工具,用于帮助开发人员消除 Java 中冗长样板代码.而我们开发人员所要做的,仅仅是添加几个 Lombok 中的注解,就可以替换掉原来的多行 get/set/toString 方法代码,既简洁也易于维护.下面我们就来看

  • Python自动安装第三方库的小技巧(pip使用详解)

    大家好,我是才哥. 最近周末也加班了,害- 有刚接触python的粉丝同学在运行此前<>的完整代码遇到以下问题,然后- 好吧,今天我们就专门介绍一下Python安装第三方库的一些小技巧,其中还包含自动安装的方法哈. 1. pip在线安装 在cmd命令行模式下,通过pip install 第三方库名称的形式,直接进行第三方库的安装. 我们以安装plotly为例,安装表现如下: pip install plotly 如果需要安装的第三方库已经存在,我们执行安装命令的时候会提示其存在且展示库安装的位

  • SpringBoot2零基础到精通之自动配置底层分析及小技巧

    目录 1 自动配置底层分析 1.1 ​自动配置注解解析 1.2 修改默认配置 1.3 总结 1.4 最佳实践 2 开发小技巧 2.1 Lombok简化开发 2.2 伪热更新 2.3 创建Spring Initailizr项目 1 自动配置底层分析 1.1 ​自动配置注解解析 @SpringBootApplication注解是SpringBoot的核心注解,他是由以下三个注解总和而成. @SpringBootConfiguration 该注解底层有一个@Configuration注解,标明当前类是

  • 5个保护MySQL数据仓库的小技巧

    汇总各种来源的数据,可以创建一个中央仓库.通过分析和汇总业务数据报告,数据仓库能够帮助企业做出明智.战略性的决策分析.虽然数据仓库提供了许多便利,但是把这些敏感数据收集到一个单独系统,会给数据仓库带来安全问题. 如果选择使用数据仓库,企业需要考虑如何更好地保护内部信息系统.任何数仓安全方面的妥协都会给入侵者或网络罪犯以可乘之机,造成销售.营销.客户信息等业务数据的毁坏泄露.今年爆发的WannaCry勒索软件事件也表明了这一点,现代企业需要严格规避数据犯罪. 在数据仓库中,最常见的数据库管理系统应

  • PHPer 需要了解的 5 个 Composer 小技巧

    Composer是新一代的PHP依赖管理工具.其介绍和基本用法可以看这篇<PHP管理依赖(dependency)关系工具 Composer 安装与使用>.本文介绍使用Composer的五个小技巧,希望能给你的PHP开发带来方便. 1. 仅更新单个库 只想更新某个特定的库,不想更新它的所有依赖,很简单: composer update foo/bar 此外,这个技巧还可以用来解决"警告信息问题".你一定见过这样的警告信息: Warning: The lock file is

  • 提高php运行速度的一些小技巧分享

    1.代码优化 代码优化不仅仅是写出干净和清晰的代码,而是对代码进行一定的简化.可以使用Zend Optimizer来自动帮助完成这些繁杂的工作.Zend Optimizer可以从Zend Technologies的网站http://www.zend.com/免费得到,但必须同意它的许可约定,因为它不是以GPL方式发行的.它的原理很简单,即通过检测Zend引擎产生的中间代码,并对它进行优化,从而获得更高的执行速度. 在使用了Zend Optimizer后,复杂的PHP源程序的执行效率马上会得到显著

  • javascript 应用小技巧方法汇总

    javascript 在前端应用体验小技巧继续积累. 事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.setCapture(); event.srcElement.releaseCapture(); 事件按键 event.keyCode event.shiftKey event.altKey event.ctrlKey 事件返回值 event.returnValue 鼠标位置 event.x e

  • Windows系统保密小技巧

    计算机的智能化既给我们带来了极大的方便,也埋下了泄密的导火索,一旦别人使用或入侵你的计算机,通过简单的查找就会发现大量信息,如已被你删除的收到和发出的邮件.访问过的Internet网站.在网页表单中输入的数据.密码及个人信息,甚至在文档中使用过的词汇等.因此,掌握一些保密技巧,保护隐私不受侵扰就显得尤为重要.下面的一些小技巧将助你营造一个更加安全的操作环境. 关闭显示最近所用文件功能 即使入侵者无法直接浏览你处理过的文档的内容,也能通过在Word或Excel的"文件"菜单中查看你最近使

  • Windows操作系统保密小技巧

    计算机的智能化既给我们带来了极大的方便,也埋下了泄密的导火索,一旦别人使用或入侵你的计算机,通过简单的查找就会发现大量信息,如已被你删除的收到和发出的邮件.访问过的Internet网站.在网页表单中输入的数据.密码及个人信息,甚至在文档中使用过的词汇等.因此,掌握一些保密技巧,保护隐私不受侵扰就显得尤为重要.下面的一些小技巧将助你营造一个更加安全的操作环境. 关闭显示最近所用文件功能 即使入侵者无法直接浏览你处理过的文档的内容,也能通过在Word或Excel的"文件"菜单中查看你最近使

  • linux下输入长文件名小技巧

    这个小技巧其实是写给新手们的,老手们恐怕会嗤之以鼻,而且我还不能算什么老手,如此看来似乎确实有些不妥.但是又转念一想即使是新手也未必不能给新手介绍一些自己的经验.俗话说的好,愚者千虑必有一得,况且大侠们对一些小技巧早已喑熟,他们恐怕不屑写这等鸡毛蒜皮一般的小文章,可是我等广大之linux 爱好而又刚刚入门者掌握这个有用的小技巧又是十分必要,因为它们可以大大简化我们的工作量.但是这方面的小技巧又很少被人提及,至少我是费了不少气力才得到的.因为这个理由才使得我有动力来写本文. 如果你是一个没有接触过

随机推荐