没使用Lombok之前,我们的源代码看起来是这一的:

public class User{
    private Long id;
    private String name;
    private int age;
    private int gender;

    public Long getId(){
        return id;
    }
    public void setId(Long id){
        this.id = id;
    }
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
    public int getAge(){
        return age;
    }
    public void setAge(int age){
        this.age = age;
    }
    public int getGender(){
        return gender;
    }
    public void setGender(int gender){
        this.gender = gender;
    }

    @Override
    public boolean equals(Object o){
        if(this == o){
            return true;
        }
        if(o == null || getClass() != o.getClass()){
            return false;
        }
        User obj =  (User) o;
        return age = obj.age &&
            gender = obj.gender &&
            Objects.equals(id,obj.id) && 
            Objects.queals(name,obj.name);
    }

    @Override
    public int hashCode(){
        return Objects.hash(id,name,age,gender);
    }

    @Override
    public String toString(){
        return "User{"+
            "id="+id+
            "name="+name+
            "age="+age+
            "gender="+gander+
            "}";
    }
}

每个JavaBean都会充斥着getter,setter,equals,hashCode和toString的模板代码,使用Lombok的 @Getter 和 @Setter 注解后,代码会像下面这样看起来很苗条:

@Getter
@Setter
public class User{
    private Long id;
    private String name;
    private int age;
    private int gender;

    @Override
    public boolean equals(Object o){
        if(this == o){
            return true;
        }
        if(o == null || getClass() != o.getClass()){
            return false;
        }
        User obj =  (User) o;
        return age = obj.age &&
            gender = obj.gender &&
            Objects.equals(id,obj.id) && 
            Objects.queals(name,obj.name);
    }

    @Override
    public int hashCode(){
        return Objects.hash(id,name,age,gender);
    }

    @Override
    public String toString(){
        return "User{"+
            "id="+id+
            "name="+name+
            "age="+age+
            "gender="+gander+
            "}";
    }
}

上面的代码仍然还有改进的空间,我们可以用@EqualsAndHashCode注解替换到equals和hashCode方法:

@Getter
@Setter
@EqualsAndHashCode
public class User{
    private Long id;
    private String name;
    private int age;
    private int gender;

    @Override
    public String toString(){
        return "User{"+
            "id="+id+
            "name="+name+
            "age="+age+
            "gender="+gander+
            "}";
    }
}

还可以使用@ToString注解去掉对toString方法:

@Getter
@Setter
@EqualsAndHashCode
@ToString
public class User{
    private Long id;
    private String name;
    private int age;
    private int gender;
}

Lombok提供了一个组合注解@Data,可以替换掉上面的注解:

@Data
public class User{
    private Long id;
    private String name;
    private int age;
    private int gender;
}

Lombok还有其他一些注解,如@Slf4j,@NoArgsConstructor,@AllArgsConstructor等等。

@Slf4j
注解在类上, 为类提供一个属性名为 log 的 log4j 的日志对象

// 使用注解
@Slf4j
@RestController
public class UserController {
	
	@RequestMapping("test")
	public void Test() {
        log.info("测试 @slf4j ");
    }
}

// 未使用注解的
@RestController
public class UserController {

	private static Logger log = LoggerFactory.getLogger(UserController.class);

	@RequestMapping("test")
	public void Test() {
        log.info("测试 @slf4j ");
    }
}

@NoArgsConstructor

// 使用注解
@NoArgsConstructor
public class User{
    private Long id;
    private String name;
    private int age;
    private int gender;
}
 
// 不使用注解
public class User{
    private Long id;
    private String name;
    private int age;
    private int gender;

	public User(){
	}
}

@AllArgsConstructor

// 使用注解
@AllArgsConstructor
public class User{
    private Long id;
    private String name;
    private int age;
    private int gender;
}
 
// 不使用注解
public class User{
    private Long id;
    private String name;
    private int age;
    private int gender;
 
    public User(Long id, String name, int age, int gender){
        this.id = id;
        this.name = name;
        this.age= age;
        this.gender = gender;
    }
}
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐