WebService:Xfire+Spring学习笔记

 
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 >
第二步:建立接口和实现类
public interface Hello {
        public String say(String name);
}
 
public class 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 中,输入你的项目地址:
看到什么了没有。有一个超链接,点击后你就可以看到 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 >
</ 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"));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值