0

我正在尝试覆盖org.apache.tapestry5.hibernate.HibernateSessionManager. 为此,我将其添加到我的 AppModule 中:

public static void contributeServiceOverride(
        MappedConfiguration<Class<?>, Object> configuration)
{
    configuration.addInstance(HibernateSessionManager.class,
            HibernateSessionManagerOverride.class);
}

HibernateSessionManagerOverride(暂时)的内容与默认的 Tapestry implementation 相同org.apache.tapestry5.internal.hibernate.HibernateSessionManagerImpl

当我运行我的应用程序时,我收到此错误:

java.lang.RuntimeException: 
Exception constructing service 'ServletApplicationInitializer': 
Unable to instantiate class org.apache.tapestry5.services.TapestryModule as a module: 
Exception constructing service 'ServiceOverride': 
Error invoking service contribution method com.company.services.AppModule.contributeServiceOverride(MappedConfiguration): 
Exception constructing service 'ServiceOverride': 
Construction of service 'ServiceOverride' has failed due to recursion:
the service depends on itself in some way. Please check org.apache.tapestry5.ioc.internal.services.ServiceOverrideImpl(Map) (at ServiceOverrideImpl.java:31) via org.apache.tapestry5.ioc.services.TapestryIOCModule.bind(ServiceBinder) (at TapestryIOCModule.java:49) for references to another service that is itself dependent on service 'ServiceOverride'.
4

2 回答 2

1

查看 hibernateModule.java,我可以看到以下内容:

@Contribute(ServiceOverride.class)
public static void provideInjectableSessionObject(
        MappedConfiguration<Class, Object> configuration, 
        @HibernateCore Session session)
{
    configuration.add(Session.class, session);
}

也许这导致了循环依赖?

您可能需要使用装饰器而不是 ServiceOverride

例如:

public static HibernateSessionManager decorateHibernateSessionManager(
        HibernateSessionManager defaultManager, 
        @Autobuild HibernateSessionManagerOverride overrideManager)
{
    return overrideManager;
}
于 2013-09-13T09:57:08.070 回答
0

我不想贬低 Lance 的答案,但使用这种方法我发现没有添加或调用 ThreadCleanupListener,这意味着会话没有在线程生命周期结束时回滚。我修改了上面的示例,将覆盖添加为线程清理侦听器。

public static HibernateSessionManager decorateHibernateSessionManager(
        HibernateSessionManager defaultManager,
        @Autobuild HibernateSessionManagerOverride overrideManager,
        PerthreadManager perthreadManager)
{
    perthreadManager.addThreadCleanupListener(overrideManager);
    return overrideManager;
}

这很关键,以便会话在使用后回滚并关闭。

于 2014-07-15T22:29:14.637 回答