在Java開發的項目里面免不了要用很多的get set 以及toString之類的方法,有時候確實是很繁瑣而且做着重復共同工作,我們有沒有辦法來簡化這個過程呢,當然有。
Lombok就可以很好的解決這個問題,它通過注解的方式來簡化消除掉一些繁瑣的Java代碼,只在編譯的過程中生成對應的方法,貼一下官網的介紹,由於是在編譯的過程中生成的這些方法,所以是不會對運行的速度產生影響的。
Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.
如何使用呢?
https://projectlombok.org/download官網或者github上面下載都可以。以eclipse為例,下載到安裝目錄下,cmd進目錄下
java - jar lombok.jar
就安裝好了
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.14</version> </dependency>
在pom.xml文件里面加入這些依賴就完成了,確實很簡單,也很方便
不是web項目怎么自動生成呢?
有時候即使不做web香菇也會有很多這種重復代碼,eclipse也有這些自動生成的功能
source>gennerate getter and setters,就可以實現這個功能了