- Lombok可以減少很多重復代碼的書寫。比如說getter/setter/toString等方法的編寫凤藏。
- IDEA安裝插件:在IDEA中雙擊Shift,搜索plugins, 點擊之后進入插件管理界面堕伪,點擊下方的Browser Repositories, 搜索lombok 揖庄,點擊安裝, 安裝完成重啟IDEA欠雌, 安裝成功
- 添加依賴
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> <scope>provided</scope> </dependency>
- 例如:
# 增加注解后再按快捷鍵 Ctrl + F12蹄梢,可以查找到set,get,toString 方法。 import lombok.Getter; import lombok.Setter; import lombok.ToString; @Getter @Setter @ToString public class User { private Integer id; private String name; private String password; public static void main(String[] args) { User u = new User(); u.setName("abc"); System.out.println(u.toString()); } }
- log注解[下面的注解會生成下方等效的java代碼]
//@CommonsLog private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class); //@JBossLog private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class); //@Log private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName()); //@Log4j private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class); //@Log4j2 private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class); //@Slf4j private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class); //@XSlf4j private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);
- @Data富俄,該注解使用在類上禁炒,該注解會提供getter、setter霍比、equals幕袱、canEqual、hashCode悠瞬、toString方法们豌。
- @AllArgsConstructor涯捻,該注解使用在類上,該注解提供一個全參數(shù)的構(gòu)造方法望迎,默認不提供無參構(gòu)造障癌。
- @NoArgsConstructor,該注解使用在類上辩尊,該注解提供一個無參構(gòu)造
- @EqualsAndHashCode 該注解使用在類上涛浙,該注解在類級別注釋會同時生成equals和hashCode。
- @NonNull 該注解使用在屬性上摄欲,該注解用于屬的非空檢查轿亮,當放在setter方法的字段上,將生成一個空檢查蒿涎,如果為空哀托,則拋出NullPointerException。
- @Cleanup劳秋,該注解使用在屬性前仓手,該注解是用來保證分配的資源被釋放。在本地變量上使用該注解玻淑,任何后續(xù)代碼都將封裝在try/finally中嗽冒,確保當前作用于中的資源被釋放。默認@Cleanup清理的方法為close补履,可以使用value指定不同的方法名稱添坊。
- @ToString 該注解使用在類上,該注解默認生成任何非講臺字段以名稱-值的形式輸出箫锤。
1贬蛙、如果需要可以通過注釋參數(shù)includeFieldNames來控制輸出中是否包含的屬性名稱。
2谚攒、可以通過exclude參數(shù)中包含字段名稱阳准,可以從生成的方法中排除特定字段。
3馏臭、可以通過callSuper參數(shù)控制是否調(diào)用父類的toString方法野蝇。 - @RequiredArgsConstructor,該注解使用在類上括儒,使用類中所有帶有 @NonNull 注解的或者帶有 final 修飾的成員變量生成對應的構(gòu)造方法绕沈。
- @Value,這個注解用在 類 上帮寻,會生成含所有參數(shù)的構(gòu)造方法乍狐,get 方法,此外還提供了equals规婆、hashCode澜躺、toString 方法蝉稳。 注意:沒有setter
- @SneakyThrows,該注解使用在方法上,這個注解用在 方法 上掘鄙,可以將方法中的代碼用 try-catch 語句包裹起來耘戚,捕獲異常并在 catch 中用 Lombok.sneakyThrow(e) 把異常拋出,可以使用 @SneakyThrows(Exception.class) 的形式指定拋出哪種異常操漠。該注解需要謹慎使用收津。詳情參看官方介紹
- @Synchronized,該注解使用在類[類級別的鎖]或者實例方法[對象鎖]上浊伙,Synchronized在一個方法上撞秋,使用關(guān)鍵字可能會導致結(jié)果和想要的結(jié)果不同,因為多線程情況下會出現(xiàn)異常情況嚣鄙。Synchronized 關(guān)鍵字將在this示例方法情況下鎖定當前對象吻贿,或者class類型的對象上多鎖定。這可能會導致死鎖現(xiàn)象哑子。一般情況下建議鎖定一個專門用于此目的的獨立鎖舅列,而不是允許公共對象進行鎖定。
- @Accessors,主要用于控制生成的getter和setter,主要參數(shù)介紹
fluent boolean值卧蜓,默認為false帐要。此字段主要為控制生成的getter和setter方法前面是否帶get/set
chain boolean值,默認false弥奸。如果設置為true榨惠,setter返回的是此對象,方便鏈式調(diào)用方法
prefix 設置前綴 例如:@Accessors(prefix = "abc") private String abcAge 當生成get/set方法時盛霎,會把此前綴去掉 - @Wither 提供了給final字段賦值的一種方法,用該注解的所有字段會構(gòu)成一個有參的構(gòu)造函數(shù)列表的構(gòu)造函數(shù)赠橙,同時每個屬性會有with打頭的方法,返回當前對象[相當于構(gòu)造函數(shù)]
- @Builder 為你的類生成復雜的構(gòu)建器API愤炸。例如:Person.builder().name("Adam Savage").city("San Francisco").job("Mythbusters").job("Unchained Reaction").build();
- @Delegate简烤,該屬性的所有對象的實例方法都將被該類代理,例如:
生成的代碼[注意toString方法下的所有方法]:import lombok.Data; import lombok.experimental.Delegate; import lombok.extern.java.Log; import java.util.ArrayList; @Data @Log public class User { private Integer id; private String name; private String password; @Delegate private ArrayList<String> collection; public static void main(String[] args) { User u = new User(); u.setName("abc"); System.out.println(u.toString()); u.log(); } public void log(){ log.fine("abc"); } }
import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Spliterator; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.UnaryOperator; import java.util.logging.Logger; public class User { private static final Logger log = Logger.getLogger(User.class.getName()); private Integer id; private String name; private String password; private ArrayList<String> collection; public static void main(String[] args) { User u = new User(); u.setName("abc"); System.out.println(u.toString()); u.log(); } public void log() { log.fine("abc"); } public User() { } public Integer getId() { return this.id; } public String getName() { return this.name; } public String getPassword() { return this.password; } public ArrayList<String> getCollection() { return this.collection; } public void setId(Integer id) { this.id = id; } public void setName(String name) { this.name = name; } public void setPassword(String password) { this.password = password; } public void setCollection(ArrayList<String> collection) { this.collection = collection; } public boolean equals(Object o) { if (o == this) { return true; } else if (!(o instanceof User)) { return false; } else { User other = (User)o; if (!other.canEqual(this)) { return false; } else { label59: { Object this$id = this.getId(); Object other$id = other.getId(); if (this$id == null) { if (other$id == null) { break label59; } } else if (this$id.equals(other$id)) { break label59; } return false; } Object this$name = this.getName(); Object other$name = other.getName(); if (this$name == null) { if (other$name != null) { return false; } } else if (!this$name.equals(other$name)) { return false; } Object this$password = this.getPassword(); Object other$password = other.getPassword(); if (this$password == null) { if (other$password != null) { return false; } } else if (!this$password.equals(other$password)) { return false; } Object this$collection = this.getCollection(); Object other$collection = other.getCollection(); if (this$collection == null) { if (other$collection != null) { return false; } } else if (!this$collection.equals(other$collection)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof User; } public int hashCode() { int PRIME = true; int result = 1; Object $id = this.getId(); int result = result * 59 + ($id == null ? 43 : $id.hashCode()); Object $name = this.getName(); result = result * 59 + ($name == null ? 43 : $name.hashCode()); Object $password = this.getPassword(); result = result * 59 + ($password == null ? 43 : $password.hashCode()); Object $collection = this.getCollection(); result = result * 59 + ($collection == null ? 43 : $collection.hashCode()); return result; } public String toString() { return "User(id=" + this.getId() + ", name=" + this.getName() + ", password=" + this.getPassword() + ", collection=" + this.getCollection() + ")"; } # 以下全是ArrayList的方法摇幻,所有方法都被當前類代理 public void trimToSize() { this.getCollection().trimToSize(); } public void ensureCapacity(int arg0) { this.getCollection().ensureCapacity(arg0); } public int size() { return this.getCollection().size(); } public boolean isEmpty() { return this.getCollection().isEmpty(); } public boolean contains(Object arg0) { return this.getCollection().contains(arg0); } public int indexOf(Object arg0) { return this.getCollection().indexOf(arg0); } public int lastIndexOf(Object arg0) { return this.getCollection().lastIndexOf(arg0); } public Object[] toArray() { return this.getCollection().toArray(); } public <T> T[] toArray(T[] arg0) { return this.getCollection().toArray(arg0); } public String get(int arg0) { return (String)this.getCollection().get(arg0); } public String set(int arg0, String arg1) { return (String)this.getCollection().set(arg0, arg1); } public boolean add(String arg0) { return this.getCollection().add(arg0); } public void add(int arg0, String arg1) { this.getCollection().add(arg0, arg1); } public String remove(int arg0) { return (String)this.getCollection().remove(arg0); } public boolean remove(Object arg0) { return this.getCollection().remove(arg0); } public void clear() { this.getCollection().clear(); } public boolean addAll(Collection<? extends String> arg0) { return this.getCollection().addAll(arg0); } public boolean addAll(int arg0, Collection<? extends String> arg1) { return this.getCollection().addAll(arg0, arg1); } public boolean removeAll(Collection<?> arg0) { return this.getCollection().removeAll(arg0); } public boolean retainAll(Collection<?> arg0) { return this.getCollection().retainAll(arg0); } public ListIterator<String> listIterator(int arg0) { return this.getCollection().listIterator(arg0); } public ListIterator<String> listIterator() { return this.getCollection().listIterator(); } public Iterator<String> iterator() { return this.getCollection().iterator(); } public List<String> subList(int arg0, int arg1) { return this.getCollection().subList(arg0, arg1); } public void forEach(Consumer<? super String> arg0) { this.getCollection().forEach(arg0); } public Spliterator<String> spliterator() { return this.getCollection().spliterator(); } public boolean removeIf(Predicate<? super String> arg0) { return this.getCollection().removeIf(arg0); } public void replaceAll(UnaryOperator<String> arg0) { this.getCollection().replaceAll(arg0); } public void sort(Comparator<? super String> arg0) { this.getCollection().sort(arg0); } }
Java 常用框架學習(Lombok)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門翼虫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人屡萤,你說我怎么就攤上這事珍剑。” “怎么了死陆?”我有些...
- 文/不壞的土叔 我叫張陵招拙,是天一觀的道長。 經(jīng)常有香客問我措译,道長别凤,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任领虹,我火速辦了婚禮规哪,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘塌衰。我一直安慰自己诉稍,他們只是感情好,可當我...
- 文/花漫 我一把揭開白布猾蒂。 她就那樣靜靜地躺著均唉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪肚菠。 梳的紋絲不亂的頭發(fā)上舔箭,一...
- 文/蒼蘭香墨 我猛地睜開眼终抽,長吁一口氣:“原來是場噩夢啊……” “哼戳表!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起昼伴,我...
- 正文 年R本政府宣布汹买,位于F島的核電站,受9級特大地震影響聊倔,放射性物質(zhì)發(fā)生泄漏晦毙。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一耙蔑、第九天 我趴在偏房一處隱蔽的房頂上張望见妒。 院中可真熱鬧,春花似錦甸陌、人聲如沸须揣。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽耻卡。三九已至,卻和暖如春牲尺,著一層夾襖步出監(jiān)牢的瞬間卵酪,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理犹赖,服務發(fā)現(xiàn),斷路器卷仑,智...
- 人在團體中或在心意女孩面前,心里都想如何才能展露頭角衬浑,成為令眾人關(guān)注的焦點捌浩!這時候?qū)W⒂诩辛Γ藭a(chǎn)生亢奮工秩!在做...
- 文/古 泉淵 “不尸饺,今天由我和奧托練習!”約瑟夫站了出來說助币。 “那可不行浪听,你會放水的!”金黃頭發(fā)眉菱、身材寬大的維綸說...