XFire跟Spring整合
XFire和Spring整合
Spring和XFire可以通过多种方式结合,下文介绍的是笔者楼主常用的一种简单而实用的方法。所用的Spring版本为2.5.6,XFire版本为1.2.6.
1.导入相关的XFire和Spring包。(注意:XFire里默认包会和Spring的包有冲突。请剔除XFire里的Spring.jar)
2.配置web.xml:
3.定义接口及实现服务
4.配置服务,将上文中实现的服务,加入到spring的配置文件中
Spring和XFire可以通过多种方式结合,下文介绍的是笔者楼主常用的一种简单而实用的方法。所用的Spring版本为2.5.6,XFire版本为1.2.6.
1.导入相关的XFire和Spring包。(注意:XFire里默认包会和Spring的包有冲突。请剔除XFire里的Spring.jar)
2.配置web.xml:
<servlet> <servlet-name>XFireServlet</servlet-name> <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <!-- 容器启动时需加载的Spring配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/applicationContext-*.xml, classpath:org/codehaus/xfire/spring/xfire.xml </param-value> </context-param> <listener> <listener-class> org.springframeword.web.context.ContextLoaderListener </listener-class> </listener>
3.定义接口及实现服务
package com.wuxw.webservices; public interface TestServicesInterface { /** * 4WebServices */ public abstract String productList(); } package com.wuxw.webservices; import java.util.List; import com.wuxw.product.model.Product; import com.wuxw.product.service.ProductService; /** * <p> * controller * </p> * * @author wuxw * @version 1.0 * @since 1.0 */ public class TestServicesController implements TestServicesInterface { /** * <p> * 注入 productService * </p> */ private ProductService productService ; /* * (non-Javadoc) * * @see * com.wuxw.webservices.WebServicesInterface#setProductService(com.wuxw. * web.service.ProductService) */ public void setProductService(ProductService productService) { this.productService = productService; } /* * (non-Javadoc) * * @see com.wuxw.webservices.WebServicesInterface#productList() */ public String productList() { System.out.println(productService); List list = productService.findAll(); Product p = new Product(); StringBuffer result = new StringBuffer(); for (int i = 0; i < list.size(); i++) { p = (Product) list.get(i); result.append(p.getName() +":"+ p.getContext() + ","); } return result.toString(); } }
4.配置服务,将上文中实现的服务,加入到spring的配置文件中
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName" > <bean name="productService" class="com.wuxw.product.service.impl.ProductServiceImpl" autowire="byName"/> <bean id="TestImpl" class="com.wuxw.webservices.TestServicesController"/> <bean name="testService" class="org.codehaus.xfire.spring.ServiceBean"> <property name="serviceBean" ref="TestImpl"></property> <property name="serviceClass" value="com.wuxw.webservices.TestServicesInterface"></property> <property name="inHandlers"> <list> <ref bean="addressingHandler"/> </list> </property> </bean> <bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler"></bean> </beans>