简体   繁体   English

使用wicket-guice时的无限循环

[英]Infinite loop when using wicket-guice

im using wicket since a couple of months now and i started to evluate wicket-guice recently. 我现在使用wicket几个月了,最近我开始躲避wicket-guice。 So im quite a guice noobie :) 所以我是一个很好的guice noobie :)

Every page got a ServiceClass (lets call it DoService) which manages a number of Objects (ie User) ... Those objects also got an reference to that ServiceClass for some reasons. 每个页面都有一个ServiceClass(让我们称之为DoService),它管理着许多对象(即用户)......由于某些原因,这些对象也得到了对该ServiceClass的引用。

public class page ... {
  @Inject
  private DoService doService;
}

public class DoService ... {
  private Collection<User> ... 
}

public class User {
   @Inject
   private DoService doService;
}

Im not quite sure, but i think the following exception occurs cause of the circular references of dependencies though i thought guice manages this all by itsself. 我不太确定,但我认为以下异常发生依赖的循环引用的原因,虽然我认为guice管理这一切都是由他自己。

Whatever.....if you need more information to understand my problem pls ask :) 无论如何......如果您需要更多信息来了解我的问题请问:)

Thanks in advance 提前致谢

java.lang.StackOverflowError
     at java.lang.reflect.InvocationTargetException.<init>(InvocationTargetException.java:54)
     at WICKET_....DoService$$FastClassByCGLIB$$ce256f9.invoke(<generated>)
     at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
     at org.apache.wicket.proxy.LazyInitProxyFactory$CGLibInterceptor.intercept(LazyInitProxyFactory.java:317)
     at WICKET_....DoService$$EnhancerByCGLIB$$d1f8934e.rollback(<generated>)
     at WICKET_c....DoService$$FastClassByCGLIB$$ce256f9.invoke(<generated>)
     at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
......

I think your design is incorrect: a domain object should not contain references to services. 我认为您的设计不正确:域对象不应包含对服务的引用。 You indeed have a circular dependency. 你确实有一个循环依赖。

What Guice does here is to create a new User instance for every DoService instance and a new DoService instance for every User instance. Guice在这里做的是为每个DoService实例创建一个新的User实例,为每个User实例创建一个新的DoService实例。 I don't think that's what you want to archive here (apart from the questionable design). 我认为这不是你想要存档的东西(除了有问题的设计)。 There are several ways around this issue: 有几种解决此问题的方法:

  1. Use appropriate Providers in your module 在模块中使用适当的提供程序
  2. Use singleton scope in guice 在guice中使用单例范围
  3. Use guice only for one of these instanciations. 仅对其中一个实例使用guice。 Have one object create the other and supply itself to the other's constructor for back-reference. 让一个对象创建另一个对象并将其自身提供给另一个构造函数以供反向引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM