这个有可能是Lombok新手使用中最常见的错误
有如下代码是引入了Lombok工具类的。
@Data
public class User {
private int id;
private String name;
private int age;
private String address;
private String mobile;
}
@Slf4j
public class HomeController {
@RequestMapping("/")
public
@ResponseBody
User home(){
User user = new User();
user.setId(1);
user.setName("youming");
log.info(user.toString());
return user;
}
}
在IntelliJ IDEA运行时找不到log
,User类的get
、set
方法也找不到,错误输出如下图所示。
开发环境:Java8+Maven+IntelliJ IDEA(预装了Lombok Plugin)。
解决思路:
1、Lombok jar是否引入的有问题?
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
查看了jar包引入没有任何问题,而且代码编辑区中的User类和HomeController类也没有报错,说明jar是正确引入的。
2、编译时出错了?
网上搜索了之后,发现有可能是编译时没有enable注解处理器。而且在我重新打开项目以后编译器也提示我需要打开Annotation Processors。
如上图所示,Annotation Processors > Enable annotation processing
。设置完成之后程序正常运行。
Lombok是如何工作的?
编译时通过注解来生成相应的代码,如最上方的两个代码块中,我使用到了@Data
和@Slf4j
,@Data
注解将会被注解处理器识别并生成getter、setter、toString、以及hashcode、equals
等方法
这个我们可以反编译一下User.class
package cn.gov.zcy.entity;
public class User {
private int id;
private String name;
private int age;
private String address;
private String mobile;
public User() {
}
public User(int id, String name, int age, String address, String mobile) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
this.mobile = mobile;
}
public User(String name, int age, String address, String mobile) {
this.name = name;
this.age = age;
this.address = address;
this.mobile = mobile;
}
public User(String name) {
this.name = name;
}
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public String getAddress() {
return this.address;
}
public String getMobile() {
return this.mobile;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setAddress(String address) {
this.address = address;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof User)) {
return false;
} else {
User other = (User)o;
if (!other.canEqual(this)) {
return false;
} else if (this.getId() != other.getId()) {
return false;
} else {
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name != null) {
return false;
}
} else if (!this$name.equals(other$name)) {
return false;
}
if (this.getAge() != other.getAge()) {
return false;
} else {
Object this$address = this.getAddress();
Object other$address = other.getAddress();
if (this$address == null) {
if (other$address != null) {
return false;
}
} else if (!this$address.equals(other$address)) {
return false;
}
Object this$mobile = this.getMobile();
Object other$mobile = other.getMobile();
if (this$mobile == null) {
if (other$mobile != null) {
return false;
}
} else if (!this$mobile.equals(other$mobile)) {
return false;
}
return true;
}
}
}
}
public boolean canEqual(Object other) {
return other instanceof User;
}
public int hashCode() {
int PRIME = true;
int result = 1;
int result = result * 31 + this.getId();
Object $name = this.getName();
result = result * 31 + ($name == null ? 0 : $name.hashCode());
result = result * 31 + this.getAge();
Object $address = this.getAddress();
result = result * 31 + ($address == null ? 0 : $address.hashCode());
Object $mobile = this.getMobile();
result = result * 31 + ($mobile == null ? 0 : $mobile.hashCode());
return result;
}
public String toString() {
return "User(id=" + this.getId() + ", name=" + this.getName() + ", age=" + this.getAge() + ", address=" + this.getAddress() + ", mobile=" + this.getMobile() + ")";
}
}
好用!强大!虽然我们也可以通过编译器去生成getter
、setter
方法,但是很多时候我们并需要知道这些方法,而这些方法放在代码里也会影响我们阅读。
当然了,我们很多时候并不需要使用@Data
这么全面的注解,我们可以按需选用@Getter
、@Setter
注解。
@Slf4j注解
package cn.gov.zcy.controller;
import cn.gov.zcy.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
private static final Logger log = LoggerFactory.getLogger(HomeController.class);
public HomeController() {
}
@RequestMapping({"/"})
@ResponseBody
public User home() {
User user = new User();
user.setId(1);
user.setName("youming");
log.info(user.toString());
return user;
}
}
private static final Logger log = LoggerFactory.getLogger(HomeController.class);
@Slf4j
会生成一个log变量,这样我们就可以直接使用log了,而无需去声明一个log。