MyEcipse实现XFire+Spring

<!--StartFragment -->

 1.MyEclipse下新建Web Service项目,选择Xfire......

 

 2.编写Service,分别写接口类和其实现类。

接口类:

package edu.tyut.caiqm.services;
//Generated by MyEclipse

public interface IHello {
 
 public String example(String message);
 
 public String sayHello(String s);
}

 

实现类:

package edu.tyut.caiqm.services;
//Generated by MyEclipse

public class HelloImpl implements IHello {
 
 public String example(String message) {
  return message;
 }

 public String sayHello(String s) {
  return "Hello, "+s+" !";
 } 
 
}

 

 3.加入对Spring的支持,这里选择Spring 1.2版本。

 

4.编写配置文件。

     4.1 Web.xml(加入对Spring的监听和XFire的Servlet)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:org/codehaus/xfire/spring/xfire.xml       
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
    
     <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
<!-- 
  <servlet>
    <servlet-name>XFireServlet</servlet-name>
    <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
  </servlet>
 
  -->
 
  <servlet>
        <servlet-name>XFireServlet</servlet-name>
        <servlet-class>
             org.springframework.web.servlet.DispatcherServlet
            <!--  org.codehaus.xfire.spring.XFireSpringServlet   -->
        </servlet-class>
    </servlet>
 
  <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

4.2  Spring配置文件:

  <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
 <bean id="mathBean" class="edu.tyut.caiqm.services.HelloImpl" />

</beans>

 

4.3 XFire服务定义配置:

文件名为:XFireServlet-servlet.xml   绿色标记部分为web.xml中org.springframework.web.servlet.DispatcherServlet 项配置的名称。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <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>
        <property name="serviceBean">
            <ref bean="mathBean" />
        </property>
        <property name="serviceClass">
            <value>edu.tyut.caiqm.services.IHello</value>                   接口的全路径
        </property>
    </bean>
</beans>

5. 目录结构为:

6. 测试部分:

6.1 先测试wsdl: 启动Tomcat后,浏览器中输入:http://localhost:9090/ServicesSpring/services/MathService?wsdl

6.2 编写测试类:

package edu.tyut.caiqm.Client;

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

import edu.tyut.caiqm.services.IHello;


public class test {
 public static void main(String [] args){
  Service servModel=new ObjectServiceFactory().create(IHello.class);
  XFireProxyFactory factory=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
   
  String endpoint="http://localhost:9090/ServicesSpring/services/MathService";
  try{
   IHello service=(IHello)factory.create(servModel, endpoint);
   String s=service.sayHello("caiqm");
   System.out.println(s);
  }catch(Exception e){
   System.out.println("error");
  }
 }
}
结果:Hello, caiqm !

你可能感兴趣的:(spring,bean,Web,servlet,MyEclipse)