前言

Github:https://github.com/HealerJean

博客:http://blog.healerjean.com

1、安装

lombok是一个可以帮助我们简化java代码编写的工具类,尤其是简化javabean的编写。

1、优点:

  即通过采用注解的方式,消除代码中的构造方法,getter/setter等代码,使我们写的类更加简洁,

2、缺点:

  当然,这带来的副作用就是不易阅读…不过,还是能看得懂吧,废话不多说,先看一下lombok支持的一些常见的注解。 

1.1、插件lombokWX20180314-192308

WX20181009-165118@2x

1.2、依赖导入

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

2、注解

2.1、@Data

注解在类上;

提供类所有属性的 gettingsetting 方法,此外还提供了equalscanEqual、hashCode、toString 方法

举例

@Data
public class EntityBean {

    String name;

    public static void main(String[] args) {
        EntityBean entityBean = new EntityBean();
        entityBean.setName("HealerJean");
        System.out.println(entityBean.getName());

    }
}

控制台 HeaelrJean

2.2、Getter和Setter

注解再字段、类上

用于生成get和set方法,默认是public的,除非向下面一样指定

public class GetSeter {
    @Getter
    @Setter
    private String name;

    @Setter(AccessLevel.PROTECTED)
    private int age;

    @Getter(AccessLevel.PUBLIC)
    private String language;

}

等价于
public class GetSeter{
    private String name;
    private int age;
    private String language;

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

    public String getName(){
        return name;
    }

    protected void setAge(int age){
        this.age = age;
    }

    public String getLanguage(){
        return language;
    }
}

2.3、@AllArgsConstructor@NoArgsConstructor

@AllArgsConstructor有参构造器

@NoArgsConstructor无参构造器

@AllArgsConstructor(access = AccessLevel.PROTECTED) //指定方法的封装为protect
@NoArgsConstructor
public class ArgsConstructor {
    private int x;

    public static void main(String[] args) {
        //有参构造器
        ArgsConstructor argsConstructor = new ArgsConstructor(2);
    }

}


等价于

public class Shape {
    private int x;
    private String name;

    public Shape(){
    }

    protected Shape(int x,String name){
 			 this.x = x;
        this.name = name;
    }
}

2.4、@log@Slf4j

@Slf4j :这个注解用在类上,可以省去从日志工厂生成日志对象这一步

@CommonsLog
private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);

@JBossLog
private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class);

@Log
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());

@Log4j
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);

@Log4j2
private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);

@Slf4j
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);

@XSlf4j
private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);


2.5、@Accessors

属性chain,用来改变set方法的void变成this.对我而言,在第一次根据别人博客学cas的时候遇到过

@Data
@Accessors(chain = true)
public class SessionKeyResult {

    private String openid;
    private String sessionKey;

    public static void main(String[] args) {
        SessionKeyResult sessionKeyResult = new SessionKeyResult();
        sessionKeyResult.setOpenid("HealerJean").setSessionKey("Jean")
    }
}


2.6、@builder

通常是 @Data@Builder 会一起用在同个类上,既方便我们流式写代码,也方便框架做事

@Builder
public class Demo {
    private Integer id;
    private String name;


    public static void main(String[] args) {
        Demo demo = Demo.builder().id(1).name("healerjean").build();
        System.out.println(demo);
    }
}

ContactAuthor