上接XFire+Spring发布Web Service(一)
2.配置文件及发布
下面就可以配置Spring和XFire,以及Web.xml等配置文件了。XFire发布Web Service有两种方式,一种是利用XFire的导出器,一种是利用在发布接口和实现类的相应位置添加注释,这种注释的标准是由BEA提出来的——JSR181,两者各有利弊,利用导出器无需对原有代码进行修改;而利用第二种方法,不适用改造现有系统,似乎只适合新开发Web Services的项目。本文暂时只讲第一种方法。
先看一下Web.xml文件的配置,重要代码添加了注释:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<!-- Spring配置文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 配合Spring容器中XFire一起工作的Servlet -->
<servlet>
<servlet-name>xfireServlet</servlet-name>
<servlet-class>
org.codehaus.xfire.spring.XFireSpringServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfireServlet</servlet-name>
<url-pattern>/service/*</url-pattern><!-- 在这个URI下开放Web Service服务 -->
</servlet-mapping>
</web-app>
接着我们来看一看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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!--引入XFire的预配置文件-->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<bean id="HelloServiceImpl" class="demo.HelloServiceImpl" />
<!-- 使用导出器导出Web Service -->
<bean id="HelloService"
class="org.codehaus.xfire.spring.remoting.XFireExporter"><!-- XFire导出器 -->
<!-- 引用XFire.xml中定义工作 -->
<property name="serviceFactory" ref="xfire.serviceFactory" />
<!-- 引用XFire.xml中定义的XFire实例 -->
<property name="xfire" ref="xfire" />
<!-- 业务服务Bean -->
<property name="serviceBean" ref="HelloServiceImpl" />
<!-- 业务服务Bean的窄接口类 -->
<property name="serviceClass"
value="demo.IHelloService" />
<!-- Web Service名称 -->
<property name="name" value="HelloServiceUT"/>
</bean>
</beans>
这样,将项目部署到tomcat下,启动容器,在浏览器中输入http://localhost:8080/springxfire/service/HelloServiceUT?wsdl,可看见该发布的Web Service的WSDL文件。
晕倒。。javaeye发文怎么限制这么严啊?稍微长一点还发不上来。。无奈,本来是一篇文章的,结果活生生被拆成三篇,刚好一节一篇,各位看的时候耐点心吧~~