tapestry 和 spring 的合并,修改hivemodule.xml如下:
即可通过下面方式进行调用。
还有一种方法:
在web.xml中添加如下代码
如下调用即可:
xml 代码
-
- <contribution configuration-id="tapestry.url.ServiceEncoders">
- <direct-service-encoder id="direct" stateless-extension="direct" stateful-extension="sdirect"/>
- <page-service-encoder id="page" extension="html" service="page"/>
- <page-service-encoder id="external" extension="external" service="external"/>
- <asset-encoder id="asset" path="/assets"/>
- <extension-encoder id="ext" extension="svc" after="*"/>
- contribution>
- <service-point id="SpringApplicationInitializer"
- interface="org.apache.tapestry.services.ApplicationInitializer"
- visibility="private">
- <invoke-factory>
- <construct class="com.javaforge.tapestry.spring.SpringApplicationInitializer">
- <set-object property="beanFactoryHolder"
- value="service:hivemind.lib.DefaultSpringBeanFactoryHolder" />
- construct>
- invoke-factory>
- service-point>
- <contribution configuration-id="tapestry.init.ApplicationInitializers">
- <command id="spring-context" object="service:SpringApplicationInitializer" />
- contribution>
即可通过下面方式进行调用。
java 代码
- @InjectObject("spring:pointsDao")
- public abstract IPointsDao getPointsDao();
还有一种方法:
java 代码
- import javax.servlet.ServletContext;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.context.ApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
- public class ServiceLocator {
- private Log logger = LogFactory.getLog(this.getClass());
- private ServletContext context = null;
- private ApplicationContext appContext;
- private static ServiceLocator instance = null;
- public ServiceLocator(ServletContext servletContext) {
- context = servletContext;
- appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(getContext());
- logger.info("Spring Service locator bean initialize ...");
- }
- public Object lookupService(String serviceBeanName) {
- return appContext.getBean(serviceBeanName);
- }
- public static synchronized ServiceLocator getInstance(ServletContext servletContext) {
- if (instance == null)
- instance = new ServiceLocator(servletContext);
- return instance;
- }
- public static synchronized ServiceLocator getInstance() {
- return instance;
- }
- public ServletContext getContext() {
- return context;
- }
- }
java 代码
- import javax.servlet.ServletException;
- import org.springframework.web.context.ContextLoaderServlet;
- public class ContextLoaderServletImpl extends ContextLoaderServlet {
- private static final long serialVersionUID = 1L;
- public void init() throws ServletException {
- super.init();
- ServiceLocator.getInstance(getServletContext());
- }
- }
在web.xml中添加如下代码
xml 代码
- <!-- Spring Config -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext-*.xml</param-value>
- </context-param>
- <servlet>
- <servlet-name>SpringContextServlet</servlet-name>
- <servlet-class>cn.com.polchina.auction.tapestry.ContextLoaderServletImpl</servlet-class>
- <load-on-startup>2</load-on-startup>
- </servlet>
如下调用即可:
java 代码
- ITest it = (ITest) ServiceLocator.getInstance().lookupService("testBean");