IntelliJ IDEA 中配置lombok插件,编写简略风格Java代码
1.打开IDEA的Settings面板,并选择Plugins选项,然后点击 “Browse repositories..”
2.开启注释处理
3.在pom.xml中添加lombox
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.16</version> </dependency>
Demo:
package com.qhong; import lombok.*; import lombok.extern.slf4j.Slf4j; import java.io.ByteArrayInputStream; import java.io.*; import java.util.ArrayList; @Data @Slf4j @AllArgsConstructor public class Something { private String name; private final String country; private final Object lockObj = new Object(); public void sayHello(@NonNull String target) { String content = String.format("hello,%s", target); System.out.println(content); log.info(content); } public void addBalabala() { val list = new ArrayList<String>(); list.add("haha"); System.out.println(list.size()); } @SneakyThrows(IOException.class) public void closeBalabala() { @Cleanup InputStream is = new ByteArrayInputStream("hello world".getBytes()); System.out.println(is.available()); } @Synchronized("lockObj") public void lockMethod() { System.out.println("test lock method"); } }
看结构:
可以发现,黄色的全是自动生成的
上面简化版的类等效于:

package com.qhong; /** * Created by qhong on 2017/6/7. */ import java.beans.ConstructorProperties; import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import lombok.NonNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Something { private static final Logger log = LoggerFactory.getLogger(Something.class); private String name; private final String country; private final Object lockObj = new Object(); public void sayHello(@NonNull String target) { if(target == null) { throw new NullPointerException("target"); } else { String content = String.format("hello,%s", new Object[]{target}); System.out.println(content); log.info(content); } } public void addBalabala() { ArrayList list = new ArrayList(); list.add("haha"); System.out.println(list.size()); } public void closeBalabala() { try { ByteArrayInputStream $ex = new ByteArrayInputStream("hello world".getBytes()); try { System.out.println($ex.available()); } finally { if(Collections.singletonList($ex).get(0) != null) { $ex.close(); } } } catch (IOException var6) { throw var6; } } public void lockMethod() { Object var1 = this.lockObj; synchronized(this.lockObj) { System.out.println("test lock method"); } } public String getName() { return this.name; } public String getCountry() { return this.country; } public Object getLockObj() { return this.lockObj; } public void setName(String name) { this.name = name; } public boolean equals(Object o) { if(o == this) { return true; } else if(!(o instanceof Something)) { return false; } else { Something other = (Something)o; if(!other.canEqual(this)) { return false; } else { label47: { String this$name = this.getName(); String other$name = other.getName(); if(this$name == null) { if(other$name == null) { break label47; } } else if(this$name.equals(other$name)) { break label47; } return false; } String this$country = this.getCountry(); String other$country = other.getCountry(); if(this$country == null) { if(other$country != null) { return false; } } else if(!this$country.equals(other$country)) { return false; } Object this$lockObj = this.getLockObj(); Object other$lockObj = other.getLockObj(); if(this$lockObj == null) { if(other$lockObj != null) { return false; } } else if(!this$lockObj.equals(other$lockObj)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof Something; } public int hashCode() { boolean PRIME = true; byte result = 1; String $name = this.getName(); int result1 = result * 59 + ($name == null?0:$name.hashCode()); String $country = this.getCountry(); result1 = result1 * 59 + ($country == null?0:$country.hashCode()); Object $lockObj = this.getLockObj(); result1 = result1 * 59 + ($lockObj == null?0:$lockObj.hashCode()); return result1; } public String toString() { return "Something(name=" + this.getName() + ", country=" + this.getCountry() + ", lockObj=" + this.getLockObj() + ")"; } @ConstructorProperties({"name", "country"}) public Something(String name, String country) { this.name = name; this.country = country; }
关于lombok的更多语法特性请参考: features
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)