WebService:Xfire+Spring学习笔记

来源:互联网 发布:Jquery 二维数组 编辑:程序博客网 时间:2024/04/30 00:45
 
WebService不太熟习,一边看网上的资料,一边自己练习写写。呵呵
1.   使用IDE: MyEclipse 6.0  MyEclipse TomCat
2.   使用jar 包:commons-loggin.jar , spring.jar
               导入 Xfire 1.2 Core Libraries
               为了进行客户端的编写导入 Xfire 1.2 HTTP Client Libraries
 
 
第一步:先把架子搭起来吧
web.xml中引入Xfire插件,是个servlet
<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>
 
 
监听器和配置文件的加载:
   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
/WEB-INF/applicationContext.xml /WEB-INF/xfire-servlet.xml
           </param-value>
    </context-param>
    <listener>
       <listener-class>
           org.springframework.web.context.ContextLoaderListener
       </listener-class>
</listener>
第二步:建立接口和实现类
publicinterface Hello {
        public String say(String name);
}
 
publicclass HelloImpl implements Hello {
        public String say(String name) {
       return name + " : 欢迎学习WebService!!";
     }
}
呵呵,简单吧。
 
第三步:文件配置
ApplicationContext.xml中注入我们的接口
<bean id="hello" class="cn.pan.com.HelloImpl"></bean>
    Xfire的也导入进来吧
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
xfire-servlet中暴露服务接口:
<bean id="echo" 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="hello"/>
       </property>
       <property name="serviceClass">
           <value>cn.pan.com.Hello</value>
       </property>
</bean>
注意这里的bean xfire xfire.serviceFactory在上面导入了xfire.xml才能被找到。
呵呵,处理器映射:
<bean class=
"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="urlMap">
           <map>
              <entry key="/hello">
                  <ref bean="echo"/>
              </entry>
           </map>
       </property>
</bean>
 
   OK,结束。部署到TOMCAT中,输入你的项目地址:
http://localhost:8080/WebService/services/Hello.html
看到什么了没有。有一个超链接,点击后你就可以看到WSDL了,WebService部署成功!!
   第四步:客户端测试:
   呵呵,差点忘了进行测试了。
   建立配置文件client.xml
<bean id = "testWebService"
class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
       <property name="serviceClass">
           <value>cn.pan.com.Hello</value>
       </property>
       <property name="wsdlDocumentUrl">
           <value>
http://localhost:8080/WebService/services/Hello?wsdl
</vale>      
       </property>
</bean>
   spring中配置客户端测试程序,你会发现程序会变得这个很简单:
 
 FileSystemXmlApplicationContext xmlContext =
new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/client.xml");
Hello h = (Hello)xmlContext.getBean("testWebService");
System.out.println(h.say("crazy jack"));