Tapestry与Spring和hibernate的集成

Tapestry4.1与Spring的集成只能通过Hivemind,幸好在Hivemind中已经预留了这个接口,具体的步骤如下:
首先,实现两个类,这两个类是用于定位BeanFactory对象的。
MyWebApplicationContextUtils类:
package com.gwssi.bjais.kernal.spring;
import org.apache.tapestry.web.WebContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* @author root
*
*/
public class MyWebApplicationContextUtils extends WebApplicationContextUtils{
public static WebApplicationContext getWebApplicationContext(WebContext wc)
{
Object obj=wc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if(obj==null)
{
return null;
}
if(obj instanceof RuntimeException)
{
throw (RuntimeException)obj;
}
if(!(obj instanceof WebApplicationContext))
{
throw new IllegalStateException("Root context attribute is not of type WebApplicationContext:"+obj);
}
return (WebApplicationContext)obj;
}
public static WebApplicationContext getRequiredWebApplicationContext(WebContext wc)
{
WebApplicationContext wac=getWebApplicationContext(wc);
if(wac==null)
{
throw new IllegalStateException("No WebApplicationContext found:no ContextLoaderListener registered?");
}
return wac;
}
}
MySpringBeanFactoryHolderImpl类:
package com.gwssi.bjais.kernal.spring;
import org.apache.hivemind.events.RegistryShutdownListener;
import org.apache.hivemind.lib.impl.SpringBeanFactoryHolderImpl;
import org.apache.tapestry.web.WebContext;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
/**
* @author root
*
*/
public class MySpringBeanFactoryHolderImpl extends SpringBeanFactoryHolderImpl implements RegistryShutdownListener{
private WebContext context;
public BeanFactory getBeanFactory()
{
if(super.getBeanFactory()==null)
{
super.setBeanFactory(MyWebApplicationContextUtils.getWebApplicationContext(this.getContext()));
}
return super.getBeanFactory();
}
public WebContext getContext()
{
return context;
}
public void setContext(WebContext context)
{
this.context=context;
}
public void registryDidShutdown()
{
((ConfigurableApplicationContext)super.getBeanFactory()).close();
}

}

第二步,在hivemind.xml配置文件中配置“hivemind.lib.DefaultSpringBeanFactoryHolder”的接口实现:
<module id="MyTapestry" version="1.0.0" package="com.gwssi.bjais.kernal.insideService">
<implementation service-id="hivemind.lib.DefaultSpringBeanFactoryHolder">
  <invoke-factory>
  <construct autowire-services="false" class="com.gwssi.bjais.kernal.spring.MySpringBeanFactoryHolderImpl">
  <event-listener service-id="hivemind.ShutdownCoordinator"/>
  <set-object property="context" value="service:tapestry.globals.WebContext"/>
  </construct>
  </invoke-factory>
  </implementation>
</module>

第三步,在page规范文件中注入spring bean:
<inject property="loginService" object="spring:loginService"/>

Tapestry与hibernate的集成就通过Spring和hibernate的集成方式就行了。

你可能感兴趣的:(apache,spring,Hibernate,Web,tapestry)