[webservices开发]集成Spring
2006-04-24 10:19
429 查看
XFire可以很好的集成到Spring中,Spring的代码已经做了这方面的集成。
首先,我们先创建我们的Web服务,采用接口和实现类的方式:
接口MathService.java:
实现类:
META-INF/xfire/service.xml文件可以省略了,因为web服务的定义在xfire-servlet.xml中可以找到。
下面要做的工具就是配置了。
在WEB-INF文件夹下创建applicationContext.xml文件,这是Spring的配置文件,如果你使用其他的Spring配置文件,可以将下面的bean添加到那个配置文件中:
定义了mathBean,这个Bean就是我们的实现类,当然你也可以在这个文件中定义其他的需要Spring管理的bean。
在WEB-INF文件夹下创建xfire-servlet.xml文件,根据Spring规范,这个文件名起做xfire-servlet.xml,其中xfire是web.xml配置的DispatcherServlet的名称:
这个文件的上半部分将MathService这个URL和math这个bean联系在一起。下半部分定义了Web服务的bean和服务接口。其中mathBean是我们在applicationContext.xml中配置的那个Bean。
最后一步就是修改web.xml文件:
需要注意这个文件的三个部分:
1. 在定义contextConfigLocation参数时一定要加上classpath:org/codehaus/xfire/spring/xfire.xml。
2. 定义listener: org.springframework.web.context.ContextLoaderListener
3. 定义DispatcherServlet: xfire
这样,你就可以访问http://localhost:8080/xfire/MathService来调用这个Web服务,也可以通过网址http://localhost:8080/xfire/MathService?wsdl来查看wsdl文档。
首先,我们先创建我们的Web服务,采用接口和实现类的方式:
接口MathService.java:
package com.kuaff.xfire.samples; public interface MathService { public long add(int p1, int p2); } |
package com.kuaff.xfire.samples; public class MathServiceImpl implements MathService { public long add(int p1, int p2) { return p1 + p2; } } |
下面要做的工具就是配置了。
在WEB-INF文件夹下创建applicationContext.xml文件,这是Spring的配置文件,如果你使用其他的Spring配置文件,可以将下面的bean添加到那个配置文件中:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="mathBean" class="com.kuaff.xfire.samples.MathServiceImpl"/> </beans> |
在WEB-INF文件夹下创建xfire-servlet.xml文件,根据Spring规范,这个文件名起做xfire-servlet.xml,其中xfire是web.xml配置的DispatcherServlet的名称:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/MathService"> <ref bean="math"/> </entry> </map> </property> </bean> <bean id="math" class="org.codehaus.xfire.spring.remoting.XFireExporter"> <property name="serviceFactory"> <ref bean="xfire.serviceFactory"/> </property> <property name="xfire"> <ref bean="xfire"/> </property> <property name="serviceBean"> <ref bean="mathBean"/> </property> <property name="serviceClass"> <value>com.kuaff.xfire.samples.MathService</value> </property> </bean> </beans> |
最后一步就是修改web.xml文件:
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml classpath:org/codehaus/xfire/spring/xfire.xml</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>xfire</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>xfire</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app> |
需要注意这个文件的三个部分:
1. 在定义contextConfigLocation参数时一定要加上classpath:org/codehaus/xfire/spring/xfire.xml。
2. 定义listener: org.springframework.web.context.ContextLoaderListener
3. 定义DispatcherServlet: xfire
这样,你就可以访问http://localhost:8080/xfire/MathService来调用这个Web服务,也可以通过网址http://localhost:8080/xfire/MathService?wsdl来查看wsdl文档。
相关文章推荐
- axis2 spring集成开发webServices
- [webservices开发]集成Spring
- [webservices开发]集成Spring
- 框架学习之Spring 第四节 Spring集成JDBC组件开发
- 11月14日filter与在web开发中集成spring培训日记
- 在Eclipse和STS集成Cloud Foundry开发环境,spring + MongoDB 示例代码演示部署(一)
- SpringMVC+Spring4+Mybatis3集成,开发简单Web项目
- xfire 集成spring的webservices
- 【持久化框架】SpringMVC+Spring4+Mybatis3集成,开发简单Web项目+源码下载
- spring-boot集成Freemarker开发
- 第八章:Shiro和Spring的集成——深入浅出学Shiro细粒度权限开发框架
- Eclipse安装CXF插件开发java web service 集成Spring
- 【持久化框架】SpringMVC+Spring4+Mybatis3集成,开发简单Web项目+源码下载
- 【持久化框架】SpringMVC+Spring4+Mybatis3集成,开发简单Web项目以及SSM框架下载
- Spring与Struts2集成开发
- Spring开发框架集成之-自定义视图
- CXF3.1.8开发与spring集成的webService
- 【持久化框架】SpringMVC+Spring4+Mybatis3集成,开发简单Web项目+源码下载
- 【持久化框架】SpringMVC+Spring4+Mybatis3集成,开发简单Web项目+源码下载
- CXF WebServices 集成Spring