簡(jiǎn)化開發(fā)|Lombok神器帶你消除冗余代碼
前言
Lombok是一款Java開發(fā)插件,使得Java開發(fā)者可以通過(guò)其定義的一些注解來(lái)消除業(yè)務(wù)過(guò)程中冗余的代碼,尤其是簡(jiǎn)單的Java模型對(duì)象(POJO)。而當(dāng)我們?nèi)绻陂_發(fā)環(huán)境中使用Lombok開發(fā)插件后,可以省出重復(fù)構(gòu)建,諸如hashCode和equals這樣的方法以及各種業(yè)務(wù)對(duì)象模型的accessor和ToString等方法的大量時(shí)間。對(duì)于這些方法,它能夠在編譯源代碼期間自動(dòng)幫我們生產(chǎn)這些方法,并沒(méi)有如反射那樣降低程序的性能。本文將帶大家詳細(xì)介紹關(guān)于Lombok的使用以及原理。
1. Lombok的概念
「概念:」
Lombok是一種Java實(shí)用工具,可以用來(lái)幫助開發(fā)人員消除冗余的代碼,對(duì)于一些簡(jiǎn)單的Java對(duì)象(POJO),它通過(guò)注釋實(shí)現(xiàn)這一目的。
2、Lombok的安裝
「安裝步驟:」
在IDEA的插件Plugins中搜索Lombok
安裝Lombok
「注意:」
在使用Lombok注解的時(shí)候記得要導(dǎo)入Lombok.jar包到工程,Maven Project的話,要在pom.xml中添加依賴
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.16.8</version>
- </dependency>
3、Lombok注解說(shuō)明
「常用注解:」
- Lombok注解說(shuō)明
- * val:用在局部變量前面,相當(dāng)于將變量聲明為final
- * @NonNull:給方法參數(shù)增加這個(gè)注解會(huì)自動(dòng)在方法內(nèi)對(duì)該參數(shù)進(jìn)行是否為空的校驗(yàn),如果為空,則拋出NPE(NullPointerException)
- * @Cleanup:自動(dòng)管理資源,用在局部變量之前,在當(dāng)前變量范圍內(nèi)即將執(zhí)行完畢退出之前會(huì)自動(dòng)清理資源,自動(dòng)生成try-finally這樣的代碼來(lái)關(guān)閉流
- * @Getter/@Setter:用在屬性上,再也不用自己手寫setter和getter方法了,還可以指定訪問(wèn)范圍
- * @ToString:用在類上,可以自動(dòng)覆寫toString方法,當(dāng)然還可以加其他參數(shù),例如@ToString(exclude=”id”)排除id屬性,或者@ToString(callSuper=true, includeFieldNames=true)調(diào)用父類的toString方法,包含所有屬性
- * @EqualsAndHashCode:用在類上,自動(dòng)生成equals方法和hashCode方法
- * @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor:用在類上,自動(dòng)生成無(wú)參構(gòu)造和使用所有參數(shù)的構(gòu)造函數(shù)以及把所有@NonNull屬性作為參數(shù)的構(gòu)造函數(shù),如果指定staticName = “of”參數(shù),同時(shí)還會(huì)生成一個(gè)返回類對(duì)象的靜態(tài)工廠方法,比使用構(gòu)造函數(shù)方便很多
- * @Data:注解在類上,相當(dāng)于同時(shí)使用了@ToString、@EqualsAndHashCode、@Getter、@Setter和@RequiredArgsConstrutor這些注解,對(duì)于POJO類十分有用
- * @Value:用在類上,是@Data的不可變形式,相當(dāng)于為屬性添加final聲明,只提供getter方法,而不提供setter方法
- * @Builder:用在類、構(gòu)造器、方法上,為你提供復(fù)雜的builder APIs,讓你可以像如下方式一樣調(diào)用Person.builder().name("Adam Savage").city("San Francisco").job("Mythbusters").job("Unchained Reaction").build();更多說(shuō)明參考Builder
- * @SneakyThrows:自動(dòng)拋受檢異常,而無(wú)需顯式在方法上使用throws語(yǔ)句
- * @Synchronized:用在方法上,將方法聲明為同步的,并自動(dòng)加鎖,而鎖對(duì)象是一個(gè)私有的屬性$lock或$LOCK,而java中的synchronized關(guān)鍵字鎖對(duì)象是this,鎖在this或者自己的類對(duì)象上存在副作用,就是你不能阻止非受控代碼去鎖this或者類對(duì)象,這可能會(huì)導(dǎo)致競(jìng)爭(zhēng)條件或者其它線程錯(cuò)誤
- * @Getter(lazy=true):可以替代經(jīng)典的Double Check Lock樣板代碼
- * @Log:根據(jù)不同的注解生成不同類型的log對(duì)象,但是實(shí)例名稱都是log,有六種可選實(shí)現(xiàn)類
- * @CommonsLog Creates log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
- * @Log Creates log = java.util.logging.Logger.getLogger(LogExample.class.getName());
- * @Log4j Creates log = org.apache.log4j.Logger.getLogger(LogExample.class);
- * @Log4j2 Creates log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
- * @Slf4j Creates log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
- * @XSlf4j Creates log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
4、Lombok代碼示范
「代碼示例:」
val將變量聲明final類型
- public static void main(String[] args) {
- val sets = new HashSet<String>();
- val lists = new ArrayList<String>();
- val maps = new HashMap<String, String>();
- //=>相當(dāng)于如下
- final Set<String> sets2 = new HashSet<>();
- final List<String> lists2 = new ArrayList<>();
- final Map<String, String> maps2 = new HashMap<>();
- }
@Nonnull為方法和構(gòu)造函數(shù)的參數(shù)提供非空檢查
- public void notNullExample(@NonNull String string) {
- string.length();
- }
- //=>相當(dāng)于
- public void notNullExample(String string) {
- if (string != null) {
- string.length();
- } else {
- throw new NullPointerException("null");
- }
- }
@Cleanup自動(dòng)釋放資源
- public static void main(String[] args) {
- try {
- @Cleanup InputStream inputStream = new FileInputStream(args[0]);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- //=>相當(dāng)于
- InputStream inputStream = null;
- try {
- inputStream = new FileInputStream(args[0]);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } finally {
- if (inputStream != null) {
- try {
- inputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
@Getter/@Setter對(duì)類的屬性字段自動(dòng)生成Get/Set方法
- @Setter(AccessLevel.PUBLIC)
- @Getter(AccessLevel.PROTECTED)
- private int id;
- private String shap;
@ToString為類生成一個(gè)toString方法
- @ToString(exclude = "id", callSuper = true, includeFieldNames = true)
- public class LombokDemo {
- private int id;
- private String name;
- private int age;
- public static void main(String[] args) {
- //輸出LombokDemo(super=LombokDemo@48524010, name=null, age=0)
- System.out.println(new LombokDemo());
- }
- }
@EqualsAndHashCode為類生成equals和hasCode方法
- @EqualsAndHashCode(exclude = {"id", "shape"}, callSuper = false)
- public class LombokDemo {
- private int id;
- private String shap;
- }
@NoArgsConstructor,@RequiredArgsConstructor and
@AllArgsConstructor,分別為類自動(dòng)生成無(wú)參構(gòu)造,指定參數(shù)構(gòu)造器和包含所有參數(shù)構(gòu)造器
- @NoArgsConstructor
- @RequiredArgsConstructor(staticName = "of")
- @AllArgsConstructor
- public class LombokDemo {
- @NonNull
- private int id;
- @NonNull
- private String shap;
- private int age;
- public static void main(String[] args) {
- new LombokDemo(1, "circle");
- //使用靜態(tài)工廠方法
- LombokDemo.of(2, "circle");
- //無(wú)參構(gòu)造
- new LombokDemo();
- //包含所有參數(shù)
- new LombokDemo(1, "circle", 2);
- }
- }
@Data在類上使用,相當(dāng)于同時(shí)使用@ToString、
@EqualsAndHashCode、@Getter、@Setter和
@RequiredArgsConstructor這些注解
- import lombok.Data;
- @Data
- public class Menu {
- private String shopId;
- private String skuMenuId;
- private String skuName;
- private String normalizeSkuName;
- private String dishMenuId;
- private String dishName;
- private String dishNum;
- //默認(rèn)閾值
- private float thresHold = 0;
- //新閾值
- private float newThresHold = 0;
- //總得分
- private float totalScore = 0;
- }
@Value為屬性添加final聲明
- @Value
- public class LombokDemo {
- @NonNull
- private int id;
- @NonNull
- private String shap;
- private int age;
- //相當(dāng)于
- private final int id;
- public int getId() {
- return this.id;
- }
- ...
- }
@Builder提供構(gòu)建值對(duì)象方式
- @Builder
- public class BuilderExample {
- private String name;
- private int age;
- @Singular
- private Set<String> occupations;
- public static void main(String[] args) {
- BuilderExample test = BuilderExample.builder().age(11).name("test").build();
- }
- }
@SneakyThrows自動(dòng)拋受檢異常
- import lombok.SneakyThrows;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- public class Test {
- @SneakyThrows()
- public void read() {
- InputStream inputStream = new FileInputStream("");
- }
- @SneakyThrows
- public void write() {
- throw new UnsupportedEncodingException();
- }
- //相當(dāng)于
- public void read() throws FileNotFoundException {
- InputStream inputStream = new FileInputStream("");
- }
- public void write() throws UnsupportedEncodingException {
- throw new UnsupportedEncodingException();
- }
- }
@Synchronized將方法聲明同步并自動(dòng)加鎖
- public class SynchronizedDemo {
- @Synchronized
- public static void hello() {
- System.out.println("world");
- }
- //相當(dāng)于
- private static final Object $LOCK = new Object[0];
- public static void hello() {
- synchronized ($LOCK) {
- System.out.println("world");
- }
- }
- }
@Getter(lazy=true)可以替代經(jīng)典的Double check Lock樣板代碼
- public class GetterLazyExample {
- @Getter(lazy = true)
- private final double[] cached = expensive();
- private double[] expensive() {
- double[] result = new double[1000000];
- for (int i = 0; i < result.length; i++) {
- result[i] = Math.asin(i);
- }
- return result;
- }
- }
- // 相當(dāng)于如下所示:
- import java.util.concurrent.atomic.AtomicReference;
- public class GetterLazyExample {
- private final AtomicReference<java.lang.Object> cached = new AtomicReference<>();
- public double[] getCached() {
- java.lang.Object value = this.cached.get();
- if (value == null) {
- synchronized (this.cached) {
- value = this.cached.get();
- if (value == null) {
- final double[] actualValue = expensive();
- value = actualValue == null ? this.cached : actualValue;
- this.cached.set(value);
- }
- }
- }
- return (double[]) (value == this.cached ? null : value);
- }
- private double[] expensive() {
- double[] result = new double[1000000];
- for (int i = 0; i < result.length; i++) {
- result[i] = Math.asin(i);
- }
- return result;
- }
- }
@Log根據(jù)不同的注解生成不同類型的log對(duì)象
- * @CommonsLog Creates log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
- * @Log Creates log = java.util.logging.Logger.getLogger(LogExample.class.getName());
- * @Log4j Creates log = org.apache.log4j.Logger.getLogger(LogExample.class);
- * @Log4j2 Creates log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
- * @Slf4j Creates log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
- * @XSlf4j Creates log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
結(jié)語(yǔ)
本篇關(guān)于Lombok的介紹就先到這里結(jié)束了,后續(xù)會(huì)出更多關(guān)于Lombok系列更多文章,謝謝大家支持!