BlogJava 聯系 聚合 管理  

          Blog Stats

          隨筆檔案(17)

          文章檔案(1)


          GaoWei


          啟動eclipse 新建一個工程test如圖:
           


          新建一個對象Account,供webservices調用
          package webServices;

          public class Account {
           
            private String name;

            public String getName() {
              return name;
            }

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

          新建一個接口
          package webServices;

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

           

          實現類
          package webServices;

          public class MathServiceImpl implements MathService{

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

          新建WEB-INF/spring.xml,一個簡單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配置相關信息
          <?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配置實現接口類-->
               <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>

          啟動web應用訪問http://localhost:8080/test/MathService?wsdl可以看見webservice配置信息


          JAVA客戶端測試
          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里面調用增加web引用
          導入相關類,直接new 用.
          import example.localhost.*;

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

           

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

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 霍邱县| 佛坪县| 阿克苏市| 兴业县| 邻水| 邵东县| 扬州市| 舟曲县| 龙口市| 揭阳市| 太白县| 汾西县| 利川市| 谷城县| 梅州市| 溆浦县| 桃江县| 长葛市| 上犹县| 东乌珠穆沁旗| 灌阳县| 普宁市| 滁州市| 嘉义市| 丁青县| 文昌市| 梅州市| 开鲁县| 泸定县| 调兵山市| 石渠县| 乌兰察布市| 博客| 安陆市| 江口县| 吴旗县| 萨嘎县| 福清市| 安仁县| 洛扎县| 达州市|