BlogJava 聯(lián)系 聚合 管理  

          Blog Stats

          隨筆檔案(17)

          文章檔案(1)


          GaoWei


          啟動(dòng)eclipse 新建一個(gè)工程test如圖:
           


          新建一個(gè)對(duì)象Account,供webservices調(diào)用
          package webServices;

          public class Account {
           
            private String name;

            public String getName() {
              return name;
            }

            public void setName(String name) {
              this.name = name;
            }
          }  

          新建一個(gè)接口
          package webServices;

          public interface MathService {
           
            Account sayHello(Account account);
          }

           

          實(shí)現(xiàn)類
          package webServices;

          public class MathServiceImpl implements MathService{

            @Override
            public Account sayHello(Account account) {
            
              account.setName("hello"+account.getName());
              return account;
            }
          }

          新建WEB-INF/spring.xml,一個(gè)簡(jiǎn)單bean配置
          <?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">
              <bean id="mathBean" class="webServices.MathServiceImpl"/>
          </beans>

          新建WEB-INF/xfire-servlet.xml,webservice配置相關(guān)信息
          <?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">
              <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>
                    <!-- spring配置實(shí)現(xiàn)接口類-->
               <property name="serviceBean">
                <ref bean="mathBean" />
               </property>
                    <!-- 接口類-->
               <property name="serviceClass">
                <value>webServices.MathService</value>
               </property>
              </bean>
          </beans>


          修改web.xml

          <?xml version="1.0" encoding="UTF-8"?>
          <web-app id="WebApp_ID" 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">
           <display-name>test</display-name>
           
           <context-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>
                     /WEB-INF/spring.xml
                     classpath:/org/codehaus/xfire/spring/xfire.xml
                  </param-value>
                 
              </context-param>

              <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>
           
           <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
           </welcome-file-list>
          </web-app>

          啟動(dòng)web應(yīng)用訪問(wèn)http://localhost:8080/test/MathService?wsdl可以看見(jiàn)webservice配置信息


          JAVA客戶端測(cè)試
          package client;

          import java.net.MalformedURLException;

          import org.codehaus.xfire.client.XFireProxyFactory;
          import org.codehaus.xfire.service.Service;
          import org.codehaus.xfire.service.binding.ObjectServiceFactory;

          import webServices.Account;
          import webServices.MathService;


          public class Client {

              /** *//**
               * @param args
               */
              public static void main(String[] args){
                  String serviceURL="http://localhost:8080/test/MathService";
                  Service serviceModel = new ObjectServiceFactory().create(MathService.class,null,serviceURL,null);
                  XFireProxyFactory serviceFactory = new XFireProxyFactory();
                  MathService service = null;   
                   try {
                      service = (MathService) serviceFactory.create(serviceModel, serviceURL);
                  
                      Account account=new Account();
                      account.setName("example");
                      System.out.println(service.sayHello(account).getName());
                   } catch (MalformedURLException e){
                      e.printStackTrace();
                  }
              }
          }


          vs2005里面調(diào)用增加web引用
          導(dǎo)入相關(guān)類,直接new 用.
          import example.localhost.*;

            AccountDao accountService = new AccountDao();
            
            textBox3.set_Text(accountService.sayHello("hhhhh"));

           

          posted on 2007-12-05 10:02 gggg874 閱讀(450) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 龙山县| 松溪县| 容城县| 云霄县| 靖州| 晋江市| 洛隆县| 西乌珠穆沁旗| 措勤县| 光山县| 苍梧县| 甘洛县| 新平| 翼城县| 景东| 麻阳| 阜新| 清原| 永宁县| 子长县| 沂水县| 长岛县| 顺平县| 泗水县| 英山县| 柯坪县| 灵川县| 瑞丽市| 哈尔滨市| 建昌县| 隆昌县| 盘锦市| 茂名市| 宽甸| 藁城市| 淄博市| 宜城市| 石城县| 司法| 二连浩特市| 竹山县|