两个类,有循环引用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
@Data public class Obj1 { private String a; private String b; private Obj2 obj2; }
@Data public class Obj2 { private String a; private String b; private Obj1 obj1; }
public class Test { public static void main(String[] args) { Obj1 obj1 = new Obj1(); Obj2 obj2 = new Obj2();
obj1.setObj2(obj2); obj2.setObj1(obj1);
System.out.println(obj1); } } |
执行上述程序,结果出现栈溢出:
首先出现上述问题的场景是出现了循环依赖。
@Data注解在什么情况下可能导致StackOverflowError情况呢?原因在于@Data重写了hashCode()方法。
我们看一下两个类的hashCode方法:
1 2 3 4 5 6 7 8 9 10 11 |
public int hashCode() { int PRIME = true; int result = 1; Object $a = this.getA(); int result = result * 59 + ($a == null ? 43 : $a.hashCode()); Object $b = this.getB(); result = result * 59 + ($b == null ? 43 : $b.hashCode()); Object $obj2 = this.getObj2(); result = result * 59 + ($obj2 == null ? 43 : $obj2.hashCode()); return result; } |
1 2 3 4 5 6 7 8 9 10 11 |
public int hashCode() { int PRIME = true; int result = 1; Object $a = this.getA(); int result = result * 59 + ($a == null ? 43 : $a.hashCode()); Object $b = this.getB(); result = result * 59 + ($b == null ? 43 : $b.hashCode()); Object $obj1 = this.getObj1(); result = result * 59 + ($obj1 == null ? 43 : $obj1.hashCode()); return result; } |
可以看到,计算obj1的hashcode,需要去获取obj2的hashcode, 而计算obj2的hashcode ,又要去获取obj1的hashcode。所以出现了死循环。
不要使用@data, 使用@Getter, @Setter。