Project Lombok—便利实用的annotation工具
Project Lombok—方便实用的annotation工具Project Lombok?项目地址:http://projectlombok.org/features/in
Project Lombok—方便实用的annotation工具
Project Lombok?
项目地址:http://projectlombok.org/features/index.html?
使用Lombok的annotation可以大大减少我们的重复性代码,使我们的代码开起来非常的简介干净。?
目前版本:0.9.3,具有以下功能特点?
1、@Getter / @Setter?
2、@Getter(lazy=true)?
3、@ToString?
4、@EqualsAndHashCode?
5、@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor?
6、@Data?
7、@Cleanup?
8、@Synchronized?
9、@SneakyThrows?
10、@Log?
11、val?
具体使用例子可以参考上面的项目地址,每一个特性都有相应的实例,这里就不再赘述了。?
在eclipse中使用Lombok:?
1、下载Lombok.jar?http://projectlombok.googlecode.com/files/lombok.jar?
2、运行Lombok.jar: java -jar? D:\001_software\work\Java\libs\lombok.jar?
3、选择eclipse安装路径?
4、安装/升级?
至此就安装好了Lombok,接下来就可以在eclipse中使用了。?
打开eclipse,创建如下的javabean:?
Java代码??
- public?@Data?class?DataObject?{??
- ????private?String?id;??
- ????private?String?name;??
- ????private?String?userId;??
- ????private?String?password;??
- }??
找到eclipse编译后生成的class文件,使用javap命令查看字节码信息如下:?
Java代码??
- public?class?com.igoolu.lombok.DataObject?extends?java.lang.Object{??
- ????public?com.igoolu.lombok.DataObject();??
- ????public?java.lang.String?toString();??
- ????public?java.lang.String?getId();??
- ????public?java.lang.String?getName();??
- ????public?java.lang.String?getUserId();??
- ????public?java.lang.String?getPassword();??
- ????public?void?setId(java.lang.String);??
- ????public?void?setName(java.lang.String);??
- ????public?void?setUserId(java.lang.String);??
- ????public?void?setPassword(java.lang.String);??
- ????public?boolean?equals(java.lang.Object);??
- ????public?int?hashCode();??
- }??
可以看到,生成的class文件中已经自动生成了setter、getter方法、toString方法、hashCode方法、equals方法。?
在maven中使用Lombok:只需要在pom.xml中增加以下内容即可?
Xml代码??
- <dependencies>??
- ????<dependency>??
- ????????<groupId>org.projectlombok</groupId>??
- ????????<artifactId>lombok</artifactId>??
- ????????<version>0.9.3</version>??
- ????????<scope>provided</scope>??
- ????</dependency>??
- </dependencies>??
- <repositories>??
- ????<repository>??
- ????????<id>projectlombok.org</id>??
- ????????<url>http://projectlombok.org/mavenrepo</url>??
- ????</repository>??
- </repositories>??
Xml代码??
- <plugin>??
- ????<groupId>org.apache.maven.plugins</groupId>??
- ????<artifactId>maven-compiler-plugin</artifactId>??
- ????<configuration>??
- ????????<compilerVersion>1.6</compilerVersion>??
- ????????<source>1.6</source>??
- ????????<target>1.6</target>??
- ????</configuration>??
- </plugin>??