分享自: http://blog.csdn.net/huey2672/article/details/42240985
Lombok是一种Java™实用工具,可用来帮助开发人员消除Java的冗长,尤其是对于简单的Java对象(POJO)。它通过注释实现这一目的。通过在开发环境中实现Lombok,开发人员可以节省构建诸如hashCode()和equals()这样的方法以及以往用来分类各种accessor和mutator的大量时间。
下载安装Lombok
1. 下载lombok.jar,http://projectlombok.org/
2. 找到MyEclipse的安装目录下的myeclipse.ini文件,在文件末尾添加以下两行:
-Xbootclasspath/a:lombok.jar
-javaagent:lombok.jar
3. 重启MyEclipse。
Lombok使用的简单示例
1. @Getter和@Setter,实现了Getter和Setter方法:
- package com.huey.dream.beans;
- import java.util.Date;
- import lombok.Getter;
- import lombok.Setter;
- public class Student {
- @Getter @Setter
- private int studNo;
- @Getter @Setter
- private String studName;
- @Getter @Setter
- private Date birthday;
- public static void main(String[] args) {
- Student stud = new Student();
- stud.setStudName("huey");
- System.out.println(stud.getStudName());
- }
- }
结果输出:
- huey
2. @EqualsAndHashCode,实现了equals()和hashCode()方法:
- package com.huey.dream.beans;
- import java.util.Date;
- import lombok.EqualsAndHashCode;
- import lombok.Getter;
- import lombok.Setter;
- @EqualsAndHashCode
- public class Student {
- @Getter @Setter
- private int studNo;
- @Getter @Setter
- private String studName;
- @Getter @Setter
- private Date birthday;
- public static void main(String[] args) {
- Date today = new Date();
- Student stud1 = new Student();
- stud1.setStudNo(10001);
- stud1.setStudName("huey");
- stud1.setBirthday(today);
- Student stud2 = new Student();
- stud2.setStudNo(10001);
- stud2.setStudName("huey");
- stud2.setBirthday(today);
- System.out.println(stud1.hashCode());
- System.out.println(stud2.hashCode());
- System.out.println(stud1.equals(stud2));
- }
- }
结果输出:
- -1678491668
- -1678491668
- true
3. @ToString,实现了toString()方法:
- package com.huey.dream.beans;
- import java.util.Date;
- import lombok.Getter;
- import lombok.Setter;
- import lombok.ToString;
- @ToString
- public class Student {
- @Getter @Setter
- private int studNo;
- @Getter @Setter
- private String studName;
- @Getter @Setter
- private Date birthday;
- public static void main(String[] args) {
- Date today = new Date();
- Student stud = new Student();
- stud.setStudNo(10001);
- stud.setStudName("huey");
- stud.setBirthday(today);
- System.out.println(stud);
- }
- }
结果输出:
- Student(studNo=10001, studName=huey, birthday=Mon Dec 29 21:04:17 CST 2014)
4. @Data, 相当上述三个annotation:
- package com.huey.dream.beans;
- import java.util.Date;
- import lombok.Data;
- @Data
- public class Student {
- private int studNo;
- private String studName;
- private Date birthday;
- public static void main(String[] args) {
- Date today = new Date();
- Student stud1 = new Student();
- stud1.setStudNo(10001);
- stud1.setStudName("huey");
- stud1.setBirthday(today);
- Student stud2 = new Student();
- stud2.setStudNo(10001);
- stud2.setStudName("huey");
- stud2.setBirthday(today);
- System.out.println(stud1.hashCode() - stud2.hashCode());
- System.out.println(stud1.equals(stud2));
- System.out.println(stud1);
- }
- }
结果输出:
- 0
- true
- Student(studNo=10001, studName=huey, birthday=Mon Dec 29 21:05:32 CST 2014)
5. @NoArgsConstructor,提供一个无参的构造方法:
- package com.huey.dream.beans;
- import java.util.Date;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- @NoArgsConstructor
- @Data
- public class Student {
- private int studNo;
- private String studName;
- private Date birthday;
- public Student(int studNo, String studName, Date birthday) {
- super();
- this.studNo = studNo;
- this.studName = studName;
- this.birthday = birthday;
- }
- public static void main(String[] args) {
- Date today = new Date();
- Student stud = new Student();
- stud.setStudNo(10001);
- stud.setStudName("huey");
- stud.setBirthday(today);
- System.out.println(stud);
- }
- }
结果输出:
- Student(studNo=10001, studName=huey, birthday=Mon Dec 29 21:15:00 CST 2014)
6. @AllArgsConstructor,提供一个全参的构造方法:
- package com.huey.dream.beans;
- import java.util.Date;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- @AllArgsConstructor
- @Data
- public class Student {
- private int studNo;
- private String studName;
- private Date birthday;
- public static void main(String[] args) {
- Date today = new Date();
- Student stud = new Student(10001, "huey", today);
- System.out.println(stud);
- }
- }
结果输出:
- Student(studNo=10001, studName=huey, birthday=Mon Dec 29 21:10:37 CST 2014)
7. @Synchronized,实现同步
- package com.huey.dream.foo;
- import java.util.Date;
- import lombok.Synchronized;
- public class Foo implements Runnable {
- @Override
- public void run() {
- sayHello();
- }
- @Synchronized
- public void sayHello() {
- System.out.println("hello, " + Thread.currentThread().getName() +"! Now is " + new Date());
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- Foo foo = new Foo();
- new Thread(foo).start();
- new Thread(foo).start();
- }
- }
结果输出:
- hello, Thread-0! Now is Mon Dec 29 21:38:26 CST 2014
- hello, Thread-1! Now is Mon Dec 29 21:38:29 CST 2014
8. @SneakyThrows,抛出异常
- package com.huey.dream.foo;
- import java.util.Date;
- import lombok.SneakyThrows;
- public class Foo {
- @SneakyThrows
- public void sayHello() {
- System.out.println("hello, " + Thread.currentThread().getName() +"! Now is " + new Date());
- Thread.sleep(3000);
- }
- public static void main(String[] args) {
- new Foo().sayHello();
- }
- }
9. @Cleanup,关闭流,如果最后清理资源的方法不是close(),可以指定,@Cleanup("clean"):
- package com.huey.dream.foo;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import lombok.Cleanup;
- public class Foo {
- public void bar(String path) throws IOException {
- File file = new File(path);
- @Cleanup
- FileReader fr = new FileReader(file);
- BufferedReader br = new BufferedReader(fr);
- // ...
- }
- }
10. @NonNull,生成一个非空检查:
- package com.huey.dream.foo;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import lombok.Cleanup;
- import lombok.NonNull;
- public class Foo {
- public void bar(@NonNull String path) throws IOException {
- File file = new File(path);
- @Cleanup
- FileReader fr = new FileReader(file);
- BufferedReader br = new BufferedReader(fr);
- // ...
- }
- }
11. @Logs,生成一个日志对象:
- package com.huey.dream.foo;
- import lombok.extern.java.Log;
- @Log
- public class Foo {
- public static void main(String[] args) {
- log.info("This is an info message.");
- }
- }
除了,Log还有其他annotation来封装一些主流的日志类库:
@CommonsLog
Creates private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(Foo.class);
@Log
Creates private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(Foo.class.getName());
@Log4j
Creates private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(Foo.class);
@Log4j2
Creates private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(Foo.class);
@Slf4j
Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Foo.class);
@XSlf4j
Creates private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(Foo.class);