@Data注解导致的StackOverflowError

简介: Springboot项目中使用Lombok,实体采用@Data注解。运行过程中报Caused by: java.lang.StackOverflowError。

场景

Springboot项目中使用Lombok,实体采用@Data注解。运行过程中报Caused by: java.lang.StackOverflowError。

@Data到底做了啥?

1、帮助我们生成Get/Set方法,简化javabean的代码冗余
2、帮助我们重写equals方法,
3、帮助我们重写hashCode
4、大大提高了JavaBean的执行效率(?)

StackOverflowError是哪里抛出的异常?

先来看StackOverflowError和OutOfMemoryError。
在《Java虚拟机规范》中描述了这两种异常:
1)如果线程请求的栈深度大于虚拟机所允许的最大深度,将抛出StackOverflowError。
2)如果虚拟机的栈内存允许动态扩展,当扩展容量无法申请到足够的内存时,将抛出OutOfMemoryError。
也就是说,由于JVM规定了栈的最大深度,因无法容纳新的栈帧而抛出StackOverflowError异常;这种情况通常预示着代码可能有出现死循环等问题。
通过查看执行log,发现TreeDTO.hashCode()方法循环抛出异常,也即出现了死循环。

@Data
public class TreeDTO implements Serializable {

    private Integer id;
    private String name;
    private Integer pid;
    private String checked;

    private List<TreeDTO> children;

}

由于该类使用了@Data注解,所以该hashCode()方法由注解自动生成,所以将范围缩小至@Data上,而且这里出现了集合间包含自身的递归引用。

什么是hashCode?

equals():是用来判断两个对象是否相同,在Object类中是通过判断对象间的内存地址来决定是否相同。
hashCode():是获取哈希码,也称为散列码,返回一个int整数。这个哈希码的作用是确定该对象在哈希表中的索引位置。如果两个对象equals()方法是相等的,那么调用二者各自的hashCode()方法必须产生同一个int结果。

为什么会出现该异常?

@Data注解编译后的Entity:

public class TreeDTO implements Serializable {
    private Integer id;
    private String name;
    private Integer pid;
    private String checked;
    private List<TreeDTO> children;

    public TreeDTO() {
    }

    public Integer getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public Integer getPid() {
        return this.pid;
    }

    public String getChecked() {
        return this.checked;
    }

    public List<TreeDTO> getChildren() {
        return this.children;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public void setChecked(String checked) {
        this.checked = checked;
    }

    public void setChildren(List<TreeDTO> children) {
        this.children = children;
    }

    public boolean equals(Object o) {
        if(o == this) {
            return true;
        } else if(!(o instanceof TreeDTO)) {
            return false;
        } else {
            TreeDTO other = (TreeDTO)o;
            if(!other.canEqual(this)) {
                return false;
            } else {
                label71: {
                    Object this$id = this.getId();
                    Object other$id = other.getId();
                    if(this$id == null) {
                        if(other$id == null) {
                            break label71;
                        }
                    } else if(this$id.equals(other$id)) {
                        break label71;
                    }

                    return false;
                }

                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;
                }

                label57: {
                    Object this$pid = this.getPid();
                    Object other$pid = other.getPid();
                    if(this$pid == null) {
                        if(other$pid == null) {
                            break label57;
                        }
                    } else if(this$pid.equals(other$pid)) {
                        break label57;
                    }

                    return false;
                }

                Object this$checked = this.getChecked();
                Object other$checked = other.getChecked();
                if(this$checked == null) {
                    if(other$checked != null) {
                        return false;
                    }
                } else if(!this$checked.equals(other$checked)) {
                    return false;
                }

                Object this$children = this.getChildren();
                Object other$children = other.getChildren();
                if(this$children == null) {
                    if(other$children == null) {
                        return true;
                    }
                } else if(this$children.equals(other$children)) {
                    return true;
                }

                return false;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof TreeDTO;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $id = this.getId();
        int result = result * 59 + ($id == null?43:$id.hashCode());
        Object $name = this.getName();
        result = result * 59 + ($name == null?43:$name.hashCode());
        Object $pid = this.getPid();
        result = result * 59 + ($pid == null?43:$pid.hashCode());
        Object $checked = this.getChecked();
        result = result * 59 + ($checked == null?43:$checked.hashCode());
        Object $children = this.getChildren();
        result = result * 59 + ($children == null?43:$children.hashCode());
        return result;
    }

    public String toString() {
        return "TreeDTO(id=" + this.getId() + ", name=" + this.getName() + ", pid=" + this.getPid() + ", checked=" + this.getChecked() + ", children=" + this.getChildren() + ")";
    }
}

由于TreeDTO中包含有自身对象的集合List,对于AbstractList的hashCode其实是把每一个子元素的hashCode经过迭代计算得到的,也就是说,要计算AbstractList的hashCode,就要把每一个子元素的hashCode先计算一遍,如果这些子元素中的某一个或子元素的子元素引用到上级对象,那么hashCode方法就会出现无限递归调用,最终出现StackOverflowError错误。
不仅仅是List集合,Set、Map、Stack也有同样的问题。

如何解决?

1、尽量不要出现集合间的递归引用。
2、使用@Getter、@Setter来替代@Data
3、@Data配合@EqualsAndHashCode(callSuper=true)一起使用,让其生成的方法中调用父类的方法。注:使用EqualsAndHashCode时,实体类必须要有继承父类,因为设置true默认是要调用父类的方法,如果没有继承,则无法使用@EqualsAndHashCode(callSuper=true),默认callSuper=false.

虽然出现这种问题的概率比较小,线上项目也是正常运行一段时间后才出现。这里不知道较高版本的JDK或者较高版本的Lombok会不会修复次问题。这里使用的是JDK-1.8以及Lombok-1.16.10。

相关文章
|
Java
Java @Data 注解详细说明
Data注解是 Lombok 提供的一个组合注解,它会为类自动生成一些常见方法的样板代码,包括 getter、setter、equals、hashCode 和 toString 方法。
3628 5
|
12月前
|
人工智能 自然语言处理 Java
AI工具选择困难症?Spring AI帮你省掉64%的令牌费用
你的AI助手有50+个工具但每次对话前就烧掉55000个令牌?就像带着全套工具箱去拧个螺丝一样浪费!Spring AI的工具搜索模式让AI按需发现工具,实现34-64%的令牌节省,告别工具选择困难症和账单焦虑。#Spring AI #工具优化 #令牌节省 #AI开发
835 2
|
自然语言处理
部落冲突脚本,小蜜脚本,赛尔号脚本开源代码
部落冲突模块包含资源自动收集和智能进攻系统,支持自定义兵种投放坐标 赛尔号模块实现精灵自动更换和战斗循环,包含颜色检测战斗状态机制
|
9月前
|
存储 人工智能 运维
企业做数据治理要多少钱?大型企业怎么做数据治理2026年实操指南
2026年,数据正式入表成核心资产。大型企业数据治理投入动辄数百万至千万,但常陷“投入无底洞”。瓴羊Dataphin以OneData方法论+AI智能体(Data Agent),提供3万–300万元/年灵活方案,TCO降低40%–65%,实现建模提效5–10倍、运维减负70%、算力节省30%,加速数据资产入表与业务变现。(239字)
|
11月前
|
人工智能 安全 Java
Java接入AI大模型:框架助力与实践指南
JBoltAI是面向Java生态的AI大模型接入框架,提供统一API、多模型适配、工程化部署与安全管控能力,支持RAG知识库集成和异步高并发处理,显著降低Java企业数智化升级门槛。(239字)
501 2
|
缓存 Java 数据库连接
Spring Boot奇迹时刻:@PostConstruct注解如何成为应用初始化的关键先生?
【8月更文挑战第29天】作为一名Java开发工程师,我一直对Spring Boot的便捷性和灵活性着迷。本文将深入探讨@PostConstruct注解在Spring Boot中的应用场景,展示其在资源加载、数据初始化及第三方库初始化等方面的作用。
654 0
|
Kubernetes 负载均衡 Cloud Native
Spring Cloud Kubernetes使用全解(一)—官方原版
Spring Cloud Kubernetes使用全解(一)—官方原版
14272 2
|
人工智能 自然语言处理 数据可视化
一键本地部署类GPT大模型,免费!
一键本地部署类GPT大模型,免费!
1128 1
SpingBoot项目使用@Data注解
1.在idea中配置lombok插件 在idea中依次点击File->settings ->Plugins
336 0
SpingBoot项目使用@Data注解
|
前端开发 Java 开发者
在Spring框架中,`PathMatcher`是用于进行URL路径匹配的接口
在Spring框架中,`PathMatcher`是用于进行URL路径匹配的接口
1177 6