您的位置:首页 > 其它

lombok使用-简介

2017-03-21 09:41 435 查看

lombok使用

lombok的使用简介

lombok其实就是在我们进行编译事加一层处理的机制,具体百度。

* https://projectlombok.org/features/index.html

* http://lrwinx.github.io/2017/03/04/%E7%BB%86%E6%80%9D%E6%9E%81%E6%81%90-%E4%BD%A0%E7%9C%9F%E7%9A%84%E4%BC%9A%E5%86%99java%E5%90%97/ 这个小哥写的不错

* http://tech.meituan.com/clean-code.html cleancode

* http://www.zuidaima.com/share/1949333085654016.htm lombok使用

常用注解

@Data :注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法

@Setter:注解在属性上;为属性提供 setting 方法

@Getter:注解在属性上;为属性提供 getting 方法

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

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

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

@AllArgsConstructor:注解在类上;为类提供一个全参的构造方法

@Accessors(chain = true) 支持链式调用的:

@Builder 建筑者模式

实战

Getter,Setter

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Getter
@Setter
public class Person {
private String name;
private String old;

public static void main(String[] args) {

Person px =new Person();
px.setName("xx");
px.setOld("xx");
log.info(px.getName());
}
}


编译后的代码:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Person {
private static final Logger log = LoggerFactory.getLogger(Person.class);
private String name;
private String old;

public Person() {
}

public static void main(String[] args) {
Person px = new Person();
px.setName("xx");
px.setOld("xx");
log.info(px.getName());
}

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

public String getOld() {
return this.old;
}

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

public void setOld(String old) {
this.old = old;
}
}


@Data

编译前:

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
* Created by wangji on 2017/3/20.
*/

@Slf4j
@Data
public class Person {
private String name;
private String old;

public static void main(String[] args) {

Person px =new Person();
px.setName("xx");
px.setOld("xx");
log.info(px.getName());
}
}


编译后的结果,可以看到给予了许多的get,set and toString等等

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Person {
private static final Logger log = LoggerFactory.getLogger(Person.class);
private String name;
private String old;

public static void main(String[] args) {
Person px = new Person();
px.setName("xx");
px.setOld("xx");
log.info(px.getName());
}

public Person() {
}

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

public String getOld() {
return this.old;
}

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

public void setOld(String old) {
this.old = old;
}

public boolean equals(Object o) {
if(o == this) {
return true;
} else if(!(o instanceof Person)) {
return false;
} else {
Person other = (Person)o;
if(!other.canEqual(this)) {
return false;
} else {
String this$name = this.getName();
String other$name = other.getName();
if(this$name == null) {
if(other$name != null) {
return false;
}
} else if(!this$name.equals(other$name)) {
return false;
}

String this$old = this.getOld();
String other$old = other.getOld();
if(this$old == null) {
if(other$old != null) {
return false;
}
} else if(!this$old.equals(other$old)) {
return false;
}

return true;
}
}
}

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

public int hashCode() {
boolean PRIME = true;
byte result = 1;
String $name = this.getName();
int result1 = result * 59 + ($name == null?43:$name.hashCode());
String $old = this.getOld();
result1 = result1 * 59 + ($old == null?43:$old.hashCode());
return result1;
}

public String toString() {
return "Person(name=" + this.getName() + ", old=" + this.getOld() + ")";
}
}


支持链式调用 @Data @Accessors(chain = true) 配合使用

类似这种类型:

Student student = new Student()
.setAge(24)
.setName("zs");


编译前:

import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

/**
* Created by wangji on 2017/3/20.
*/

@Slf4j
@Data
@Accessors(chain = true)
public class Person {
private String name;
private String old;

public static void main(String[] args) {

Person px =new Person().setOld("xxx").setName("xxx");
log.info(px.getName());
}
}


编译后的代码:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Person {
private static final Logger log = LoggerFactory.getLogger(Person.class);
private String name;
private String old;

public static void main(String[] args) {
Person px = (new Person()).setOld("xxx").setName("xxx");
log.info(px.getName());
}

public Person() {
}

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

public String getOld() {
return this.old;
}

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

public Person setOld(String old) {
this.old = old;
return this;
}

public boolean equals(Object o) {
if(o == this) {
return true;
} else if(!(o instanceof Person)) {
return false;
} else {
Person other = (Person)o;
if(!other.canEqual(this)) {
return false;
} else {
String this$name = this.getName();
String other$name = other.getName();
if(this$name == null) {
if(other$name != null) {
return false;
}
} else if(!this$name.equals(other$name)) {
return false;
}

String this$old = this.getOld();
String other$old = other.getOld();
if(this$old == null) {
if(other$old != null) {
return false;
}
} else if(!this$old.equals(other$old)) {
return false;
}

return true;
}
}
}

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

public int hashCode() {
boolean PRIME = true;
byte result = 1;
String $name = this.getName();
int result1 = result * 59 + ($name == null?43:$name.hashCode());
String $old = this.getOld();
result1 = result1 * 59 + ($old == null?43:$old.hashCode());
return result1;
}

public String toString() {
return "Person(name=" + this.getName() + ", old=" + this.getOld() + ")";
}
}


@builder

Builder模式,可以看一下《Head First》(设计模式) 的建造者模式。

一种变种的builder模式,那就是构建bean的builder模式,其实主要的思想是带着大家一起看一下lombok给我们带来了什么。

看一下Student这个类的原始builder状态:

public class Student {
private String name;
private int age;

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 static Builder builder(){
return new Builder();
}
public static class Builder{
private String name;
private int age;
public Builder name(String name){
this.name = name;
return this;
}

public Builder age(int age){
this.age = age;
return this;
}

public Student build(){
Student student = new Student();
student.setAge(age);
student.setName(name);
return student;
}
}

}


调用的方法:

Student student = Student.builder().name("zs").age(24).build();


这样的builder代码,让我是在恶心难受,于是我打算用lombok重构这段代码:

@Builder
public class Student {
private String name;
private int age;
}
Student student = Student.builder().name("zs").age(24).build();


上面的是参考的,下面是自己实现的

import lombok.Builder;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

/**
* Created by wangji on 2017/3/20.
*/

@Slf4j
@Builder
@ToString
public class Person {
private String name;
private String old;

public static void main(String[] args) {

Person person =Person.builder().name("xxx").old("xx").build();
log.info(person.toString());
}
}


编译后的代码:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Person {
private static final Logger log = LoggerFactory.getLogger(Person.class);
private String name;
private String old;

public static void main(String[] args) {
Person person = builder().name("xxx").old("xx").build();
log.info(person.toString());
}

Person(String name, String old) {
this.name = name;
this.old = old;
}

public static Person.PersonBuilder builder() {
return new Person.PersonBuilder();
}

public String toString() {
return "Person(name=" + this.name + ", old=" + this.old + ")";
}

public static class PersonBuilder {
private String name;
private String old;

PersonBuilder() {
}

public Person.PersonBuilder name(String name) {
this.name = name;
return this;
}

public Person.PersonBuilder old(String old) {
this.old = old;
return this;
}

public Person build() {
return new Person(this.name, this.old);
}

public String toString() {
return "Person.PersonBuilder(name=" + this.name + ", old=" + this.old + ")";
}
}
}


@NoArgsConstructor:注解在类上;为类提供一个无参的构造方法 @AllArgsConstructor:注解在类上;为类提供一个全参的构造方法

这个没啥好说的!

每天都是学习!哈哈!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: