安装步骤:
步骤一:lombok的下载地址为:https://projectlombok.org/download,jar包很小。这里也把依赖写出来:
org.projectlombok
lombok
1.18.2
provided
步骤二:双击jar包,jar包内的安装器会自动运行寻找eclipse
步骤三:选择需要安装的eclipse,然后点击安装(Install/Update),下一步会提示安装成功。
这就完成了安装,是不是很简单,我们看一下,安装过程具体操作了什么:
原来是做了两部分,第一,把jar包复制到了eclipse安装目录,第二在eclipse.ini文件中加入了一句话:-javaagent:D:\eclipse_4.4\eclipse\lombok.jar
接下来就是测试一下了,但是博主在这遇到了问题,耗费了一点时间来解决。
测试步骤:
重启eclipse,将jar包导入到工程,写测试类,在这遇到了错误:
报错,这说明注解没起作用,这就纳闷了,我重新按步骤做了一遍,然后切换了lombok版本,这些方法都试了,结果还是不行,偶然注意到了下面这句:
对,就是安装成功的提示,后面标红的没问题,这是具体jar工作的的一环,但是前面标红这句,我看了一下eclipse.ini文件,还真没有,我抱着试试原则,结果就是这个原因,注解起作用了。
最后看一下源文件和编译后的文件。
Test.java:
packagetest;importlombok.Data;
@Datapublic classTest {privateString name;privateString age;public static voidmain(String[] args) {
Test se= newTest();
se.setName("zhangsan");
se.setAge("16");
System.out.println(se.getAge());
System.out.println(se.getName());
}
}
Test.class:
packagetest;public classTest {privateString name;privateString age;public static voidmain(String[] args) {
Test se= newTest();
se.setName("zhangsan");
se.setAge("16");
System.out.println(se.getAge());
System.out.println(se.getName());
}publicString getName() {return this.name;
}publicString getAge() {return this.age;
}public voidsetName(String name) {this.name =name;
}public voidsetAge(String age) {this.age =age;
}public booleanequals(Object o) {if (o == this) {return true;
}else if (!(o instanceofTest)) {return false;
}else{
Test other=(Test) o;if (!other.canEqual(this)) {return false;
}else{
Stringthis$name = this.getName();
String other$name=other.getName();if (this$name == null) {if (other$name != null) {return false;
}
}else if (!this$name.equals(other$name)) {return false;
}
Stringthis$age = this.getAge();
String other$age=other.getAge();if (this$age == null) {if (other$age != null) {return false;
}
}else if (!this$age.equals(other$age)) {return false;
}return true;
}
}
}protected booleancanEqual(Object other) {return other instanceofTest;
}public inthashCode() {boolean PRIME = true;byte result = 1;
String $name= this.getName();int result1 = result * 59 + ($name == null ? 43: $name.hashCode());
String $age= this.getAge();
result1= result1 * 59 + ($age == null ? 43: $age.hashCode());returnresult1;
}publicString toString() {return "Test(name=" + this.getName() + ", age=" + this.getAge() + ")";
}
}