- 論壇徽章:
- 0
|
在寫(xiě)使用Java時(shí),難免會(huì)有一些模板代碼要寫(xiě),不然get/set,toString, hashCode, close 資源,定義構(gòu)造函數(shù)等等。代碼會(huì)顯得很冗余,很長(zhǎng)。Lombok項(xiàng)目可以是我們擺脫這些東西,通過(guò)一系列的注解,Lombok可以幫我們自動(dòng)生成這些函數(shù)。
Lombok 官網(wǎng)地址:https://projectlombok.org/
參考文檔:https://projectlombok.org/features/index.html
1. 安裝
到官網(wǎng)下載 lombok.jar,直接雙擊,按照提示進(jìn)行操作,就可以在eclipse中安裝成功。
如果使用maven時(shí),則需要引入依賴:- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.16.4</version>
- <scope>provided</scope>
- </dependency>
復(fù)制代碼 如果需要用javac或者其他命令工具編譯java類(lèi),則需要將 lombok.jar放入classpath.
2. 使用方法 (文檔:https://projectlombok.org/features/index.html)
1> @Getter/@Setter, 注解在一個(gè)pojo類(lèi)上,會(huì)在編譯時(shí),幫我們自動(dòng)生成get/set函數(shù)。
2> @ToString 注解在類(lèi)上,編譯時(shí),幫我們生成包括所有field的toString函數(shù);
3> @EqualsAndHashCode, 編譯時(shí),幫我們生成equlas 和hashCode函數(shù);
4> @Cleanup, 注解在一些資源對(duì)象的定義上,可以幫我們自動(dòng)調(diào)用它們的close()函數(shù);這個(gè)很有幫助;
5> @NoArgsContructor,@RequireArgsContructor, @AllArgsContructor,分別幫我們生成無(wú)參數(shù)構(gòu)造函數(shù),每一個(gè)非Null的field的構(gòu)造函數(shù),所有field參數(shù)的構(gòu)造函數(shù);
6> @Data,All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor! (等價(jià)于:@ToString, @EqualsAndHashCode, @Getter, @Setter, @RequiredArgsConstructor)
更多的注解,參見(jiàn)https://projectlombok.org/features/index.html
3. 例子- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class Test {
- private int id;
- private String name;
- private String password;
- public static void main(String[] args) {
- Test test = new Test(1, "test", "password");
- System.out.println(test);
- System.out.println(test.getName());
- }
- }
復(fù)制代碼 結(jié)果:- Test(id=1, name=test, password=password)
- test
復(fù)制代碼 通過(guò)@Data, @AllArgsConstructor,@NoArgsConstructor 三個(gè)注解自動(dòng) 生成了 Test 的全field參數(shù)的構(gòu)造函數(shù),自動(dòng)生成了 toString(), get/set函數(shù)等等。
再看一例:- public static void main(String[] args) throws IOException{
- @Cleanup
- InputStream in = new FileInputStream("/home/a.txt");
- @Cleanup
- OutputStream out = new FileOutputStream("/home/b.txt");
- byte[] b = new byte[10000];
- while (true) {
- int r = in.read(b);
- if (r == -1)
- break;
- out.write(b, 0, r);
- }
- }
復(fù)制代碼 @Cleanup自動(dòng)幫我們調(diào)用 close() 方法進(jìn)行關(guān)閉資源。
You can use @Cleanup to ensure a given resource is automatically cleaned up before the code execution path exits your current scope. You do this by annotating any local variable declaration with the @Cleanup annotation like so:
@Cleanup InputStream in = new FileInputStream("some/file");
As a result, at the end of the scope you're in, in.close() is called. This call is guaranteed to run by way of a try/finally construct.
If the type of object you'd like to cleanup does not have a close() method, but some other no-argument method, you can specify the name of this method like so:
@Cleanup("dispose") org.eclipse.swt.widgets.CoolBar bar = new CoolBar(parent, 0);
By default, the cleanup method is presumed to be close(). A cleanup method that takes 1 or more arguments cannot be called via @Cleanup.
@Cleanup是通過(guò) try/finally 實(shí)現(xiàn)的,如果資源的關(guān)閉方法不是默認(rèn)的close(),那么也可以指定關(guān)閉方法的名稱@Cleanup("closeMethod"), 但是關(guān)閉方法不能有參數(shù),不然就無(wú)法使用 @Cleanup了。
更多的 參考 https://projectlombok.org/features/index.html
通過(guò)使用 Lombok,可以減少很多的 Java 代碼的,減輕了心理負(fù)擔(dān)。 |
|