XFire + Spring 编写webservice

发布时间:2014-10-25 2:24:11
来源:分享查询网

 1、java代码:         IHello.java         package example;       public interface IHello        {             public String hello();         }            package example;          HelloImpl.java        public class HelloImpl implements IHello         {             public String hello()            {                  return "hello";            }         }   2、配置文件:     web.xml(注意:classpath:org/codehaus/xfire/spring/xfire.xml 必须加进去)   <?xml version="1.0" encoding="UTF-8"?>   <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">     <!-- 配置文件路径 开始 -->       <context-param>           <param-name>log4jConfigLocation</param-name>           <param-value>/WEB-INF/classes/log4j.properties</param-value>       </context-param>       <context-param>           <param-name>contextConfigLocation</param-name>           <param-value>               /WEB-INF/classes/applicationContext*.xml               classpath:org/codehaus/xfire/spring/xfire.xml           </param-value>       </context-param>              <!-- 启动时加载SpringContextServlet -->       <listener>           <listener-class>               org.springframework.web.context.ContextLoaderListener           </listener-class>       </listener>       <listener>            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>        </listener>           <listener>           <listener-class>               org.springframework.web.util.IntrospectorCleanupListener           </listener-class>       </listener>                <!-- XFire 配置 -->         <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>*.ws</url-pattern>         </servlet-mapping>                  <welcome-file-list>           <welcome-file>index.html</welcome-file>         </welcome-file-list>   </web-app>     xfire-servlet.xml (新建这个文件,并且和web.xml放在同一个文件夹) <?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="/testService.ws">                       <ref bean="test"/>                     </entry>                </map>            </property>         </bean>                   <bean id="test" parent="webService" class="org.codehaus.xfire.spring.remoting.XFireExporter">            <property name="serviceBean">                <ref bean="helloBean"/>            </property>            <property name="serviceClass">                <value>example.IHello</value>            </property>          </bean>                     <!-- webService base -->         <bean id="webService" class="org.codehaus.xfire.spring.remoting.XFireExporter" abstract="true">           <property name="serviceFactory">               <ref bean="xfire.serviceFactory" />           </property>           <property name="xfire">               <ref bean="xfire" />           </property>        </bean>   </beans>      spring 的配置文件 applicationContext-webService.xml <?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="helloBean" class="example.HelloImpl"></bean>   </beans>          

返回顶部
查看电脑版