在去北京培训的时候,讲师说到了
lombok
这个第三方插件包,使用了以后发现,确实是个神奇,避免了编写不少臃肿的且定式的代码,虽然现代的IDE
都能经过快捷键或者右键的方式,使用Generate Getters and Setters
快速生成setters/getters,但当某一个字段修改或者添加字段时,又须要重复的操做一遍,但使用了lombok
以后。一切都是自动的,除了最经常使用的生成setters/getters
,还有诸如:自动生成toString
方法、equals
、·haashcode·等,还能快速生成Builder模式
的javabean类,实在是方便。程序猿是很懒的,一切重复的工做都想经过脚本或者自动化工具来完成,因此,使用lombok
吧。html
咱们在开发过程当中,一般都会定义大量的JavaBean,而后经过IDE去生成其属性的构造器、getter、setter、equals、hashcode、toString方法,当要增长属性或者对某个属性进行改变时,好比命名、类型等,都须要从新去生成上面提到的这些方法。这样重复的劳动没有任何意义,Lombok里面的注解能够轻松解决这些问题。java
Lombok是一个能够经过简单的注解形式来帮助咱们简化消除一些必须有但显得很臃肿的Java代码的工具,经过使用对应的注解,能够在编译源码的时候生成对应的方法。git
官方地址:https://projectlombok.org/ github地址:https://github.com/rzwitserloot/lombokgithub
官网对其解释为: spring
这里简单说下
lombok
实现的原理:主要是经过抽象语法树(AST)
,在编译处理后,匹配到有其注解的类,那么注解编译器就会自动去匹配项目中的注解对应到在lombok语法树中的注解文件,并通过自动编译匹配来生成对应类中的getter或者setter方法,达到简化代码的目的。缓存
利用此原理,也可自行编写一些工做中一些常常使用到的,好比实体类转Map对象,map对象转实体类,本来使用Beanutils
或者cglib的BeanCopier
实现转换,前者使用的是反射的机制,因此性能相对较差,后者是使用修改字节码技术,性能在未使用Converter
时基本等同于set
和get
方法。但说白了仍是麻烦,毕竟还须要缓存对象等作到复用等。而使用lombok
的形式的话,一切都是自动的,性能基本是没有损失的,因为对AST
不熟悉,以后有时间了能够进行插件编写下(去官网提过这个问题,官方回复说,不太符合lombok
的使用场景,⊙﹏⊙‖∣,仍是本身动手,风衣足食吧~)springboot
lombok.jar
包,会自动扫描系统的ide安装状况(或者手动指定目录),点击Install/Update
,便可。eclipse.ini
文件,设置javaagent
属性便可(第二种方法最后的效果也是这样的。):<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.20</version> </dependency>
@Getter / @Setter
:能够做用在类上和属性上,放在类上,会对全部的非静态(non-static)属性生成Getter/Setter方法,放在属性上,会对该属性生成Getter/Setter方法。并能够指定Getter/Setter方法的访问级别。微信
@EqualsAndHashCode
:默认状况下,会使用全部非瞬态(non-transient)和非静态(non-static)字段来生成equals和hascode方法,也能够指定具体使用哪些属性。 @ToString 生成toString方法,默认状况下,会输出类名、全部属性,属性会按照顺序输出,以逗号分割。eclipse
@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
:无参构造器、部分参数构造器、全参构造器maven
** @Data:包含@ToString, @EqualsAndHashCode, 全部属性的@Getter, 全部non-final属性的@Setter和@RequiredArgsConstructor的组合,一般状况下,基本上使用这个注解就足够了。**
@Budilder
:能够进行Builder方式初始化。
@Slf4j
:等同于:private final Logger logger = LoggerFactory.getLogger(XXX.class);简直不能更爽了!通常上用在其余java类上
更多注解说明,可查看:https://projectlombok.org/features/index.html
使用lombok
@Data @Builder @NoArgsConstructor @AllArgsConstructor public class Demo { String code; String name; }
等同于
public class Demo { String code; String name; public static DemoBuilder builder() { return new DemoBuilder(); } public String getCode() { return this.code; } public String getName() { return this.name; } public void setCode(String code) { this.code = code; } public void setName(String name) { this.name = name; } public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Demo)) return false; Demo other = (Demo) o; if (!other.canEqual(this)) return false; Object this$code = getCode(); Object other$code = other.getCode(); if (this$code == null ? other$code != null : !this$code.equals(other$code)) return false; Object this$name = getName(); Object other$name = other.getName(); return this$name == null ? other$name == null : this$name.equals(other$name); } protected boolean canEqual(Object other) { return other instanceof Demo; } public int hashCode() { int PRIME = 59; int result = 1; Object $code = getCode(); result = result * 59 + ($code == null ? 43 : $code.hashCode()); Object $name = getName(); return result * 59 + ($name == null ? 43 : $name.hashCode()); } public String toString() { return "Demo(code=" + getCode() + ", name=" + getName() + ")"; } public Demo() { } public Demo(String code, String name) { this.code = code; this.name = name; } public static class DemoBuilder { private String code; private String name; public DemoBuilder code(String code) { this.code = code; return this; } public DemoBuilder name(String name) { this.name = name; return this; } public Demo build() { return new Demo(this.code, this.name); } public String toString() { return "Demo.DemoBuilder(code=" + this.code + ", name=" + this.name + ")"; } } }
使用@Slf4j
(摘抄至官网)
@Slf4j public class LogExampleOther { public static void main(String... args) { log.error("Something else is wrong here"); } }
常规的
public class LogExampleOther { private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleOther.class); public static void main(String... args) { log.error("Something else is wrong here"); } }
省了多少事!!!少年快使用吧!
499452441
lqdevOps
本文地址:https://blog.lqdev.cn/2018/07/12/springboot/chapter-two/