转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/80743302
本文出自【赵彦军的博客】
lombok
是一个编译级别的插件,它可以在项目编译的时候生成一些代码。比如日常开发过程中需要生产大量的JavaBean
文件,每个JavaBean
都需要提供大量的get
和set
方法,如果字段较多且发生变动的话修改起来相对繁琐,相应的lombok
可以通过注解(@getter,@setter)
为我们省去手动创建getter
和setter
方法的麻烦,它能够在我们编译源码的时候自动帮我们生成getter
和setter
方法。
即它最终能够达到的效果是:在源码中没有getter
和setter
方法,但是在编译生成的字节码文件中有getter
和setter
方法。
另外在项目开发阶段,一个class
的属性是一直变化的,今天可能增加一个字段,明天可能删除一个字段。每次变化都需要修改对应的模板代码。另外,有的class
的字段超级多,多到一眼看不完。如果加上模板代码,更难一眼看出来。更有甚者,由于字段太多,想要使用builder
来创建。手动创建builder
和字段和原来的类夹杂在一起,看起来很凌乱。lombok
的@Builder
即可解决这个问题。
官网地址:https://projectlombok.org/
lombok最新版本号:http://jcenter.bintray.com/org/projectlombok/lombok/
官方文档: https://projectlombok.org/features/all
lombok 注解介绍: http://blog.csdn.net/sunsfan/article/details/53542374
添加注解支持
buildscript {
ext {
springBootVersion = '2.0.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.yanjun'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter',
'org.springframework.boot:spring-boot-starter-web',
)
//添加lombok依赖
compile 'org.projectlombok:lombok:1.18.0'
}
package com.yanjun.mybatis.bean;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
@Data
@NoArgsConstructor
public class User {
public Integer age;
@NonNull
public String name;
}
相当于
package com.yanjun.mybatis.bean;
import lombok.NonNull;
public class User {
public Integer age;
@NonNull
public String name;
public Integer getAge() {
return this.age;
}
@NonNull
public String getName() {
return this.name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setName(@NonNull String name) {
if (name == null) {
throw new NullPointerException("name is marked @NonNull but is null");
} else {
this.name = name;
}
}
}
测试
User user = new User();
user.setName(null);
效果
Exception in thread "main" java.lang.NullPointerException: name is marked @NonNull but is null
at com.yanjun.mybatis.bean.User.setName(User.java:7)
at com.yanjun.mybatis.LombokApplication.main(LombokApplication.java:15)
package com.yanjun.mybatis.bean;
import lombok.Data;
@Data //自动生成 get、set 方法
public class User {
Integer id;
String name;
Integer age;
public static void main(String[] args) {
//测试方法
User user = new User();
user.setName("zhaoyanjun");
user.setAge(20);
user.setId(1);
}
}
package com.yanjun.mybatis.bean;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Data //自动生成 get、set 方法
@Slf4j //日志打印
public class User {
Integer id;
String name;
Integer age;
public static void main(String[] args) {
User user = new User();
user.setName("zhaoyanjun");
user.setAge(20);
user.setId(1);
log.info("日志" + user.toString());
}
}
package com.yanjun.mybatis.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Data //自动生成 get、set 方法
@AllArgsConstructor //自动生成全参数构造函数
@Slf4j //日志打印
public class User {
Integer id;
String name;
Integer age;
public static void main(String[] args) {
User user = new User(1, "zhaoyanjun", 20);
log.info("日志" + user.toString());
}
}
package com.yanjun.mybatis.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
@Data //自动生成 get、set 方法
@AllArgsConstructor //自动生成全参数构造函数
@Slf4j //日志打印
@ToString(of = {"id","age"}) //tostring() 方法里面只打印 id ,name 字段
public class User {
Integer id;
String name;
Integer age;
public static void main(String[] args) {
User user = new User(1, "zhaoyanjun", 20);
log.info("日志" + user.toString());
}
}
toString() 方法排除字段
//排除字段
@ToString(exclude = {"name"}) //tostring() 方法中,name 字段不参与打印
@Value
注解和@Data
类似,区别在于它会把所有成员变量默认定义为 private final
修饰,并且不会生成set
方法。
package com.yanjun.mybatis.bean;
import lombok.Value;
@Value
public class User {
Integer id = 1;
String name = "zhaoyanjun";
Integer age = 3 ;
}
编译后的代码
public final class User {
private final Integer id = 1;
private final String name = "zhaoyanjun";
private final Integer age = 3;
public User() {
}
public Integer getId() {
return this.id;
}
public String getName() {
this.getClass();
return "zhaoyanjun";
}
public Integer getAge() {
return this.age;
}
//省略部分代码
......
}
import lombok.Builder;
@Builder
public class User {
public Integer id;
public String name;
public Integer age;
}
相当于以下代码
package com.yanjun.mybatis.bean;
public class User {
public Integer id;
public String name;
public Integer age;
User(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public static User.UserBuilder builder() {
return new User.UserBuilder();
}
public static class UserBuilder {
private Integer id;
private String name;
private Integer age;
UserBuilder() {
}
public User.UserBuilder id(Integer id) {
this.id = id;
return this;
}
public User.UserBuilder name(String name) {
this.name = name;
return this;
}
public User.UserBuilder age(Integer age) {
this.age = age;
return this;
}
public User build() {
return new User(this.id, this.name, this.age);
}
public String toString() {
return "User.UserBuilder(id=" + this.id + ", name=" + this.name + ", age=" + this.age + ")";
}
}
}
使用
UserBuilder userBuilder = User.builder();
User user = userBuilder
.age(10)
.id(1)
.name("yanjun")
.build();
System.out.println(": " + userBuilder.toString());
package com.yanjun.mybatis.bean;
import lombok.Synchronized;
public class User {
public Integer age;
public String name;
//普通方法,相当于对象锁
@Synchronized
int run1() {
return 1;
}
//静态方法,相当于类锁
@Synchronized
static int run2() {
return 2;
}
}
代码的效果相当于
public class User {
private final Object $lock = new Object[0];
private static final Object $LOCK = new Object[0];
public Integer age;
public String name;
public User() {
}
int run1() {
Object var1 = this.$lock;
synchronized(this.$lock) {
return 1;
}
}
static int run2() {
Object var0 = $LOCK;
synchronized($LOCK) {
return 2;
}
}
}
个人微信号:zhaoyanjun125 , 欢迎关注