spring与xfire结合

以前用xfrie,感觉不太好懂,现在用spring+xfire感觉很好理解。下面是个hello的例子。

IHello.java

java 代码
  1. package test;   
  2.   
  3. public interface IHello {   
  4.     public String helloTo(String name);   
  5. }   

HelloImpl.java

java 代码
  1. package test;   
  2.   
  3. public class HelloImpl implements IHello {   
  4.   
  5.     public String helloTo(String name) {   
  6.         return "hello " + name + "!";   
  7.     }   
  8. }   

applicationContext.xml

xml 代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">  
  3. <beans>  
  4.   
  5.     <bean  
  6.         class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  7.         <property name="urlMap">  
  8.             <map>  
  9.                 <entry key="/testService.ws">  
  10.                     <ref bean="test" />  
  11.                 </entry>  
  12.             </map>  
  13.         </property>  
  14.     </bean>  
  15.   
  16.     <bean id="test" parent="webService"  
  17.         class="org.codehaus.xfire.spring.remoting.XFireExporter">  
  18.         <property name="serviceBean">  
  19.             <ref bean="testBean" />  
  20.         </property>  
  21.         <property name="serviceClass">  
  22.             <value>test.IHello</value>  
  23.         </property>  
  24.     </bean>  
  25.   
  26.     <!-- webService base -->  
  27.     <bean id="webService"  
  28.         class="org.codehaus.xfire.spring.remoting.XFireExporter"  
  29.         abstract="true">  
  30.         <property name="serviceFactory">  
  31.             <ref bean="xfire.serviceFactory" />  
  32.         </property>  
  33.         <property name="xfire">  
  34.             <ref bean="xfire" />  
  35.         </property>  
  36.     </bean>  
  37.   
  38.     <bean id="testBean" class="test.HelloImpl"></bean>  
  39. </beans>  

web.xml

java 代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  5.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   
  6.   
  7.     <context-param>   
  8.         <param-name>contextConfigLocation</param-name>   
  9.         <param-value>   
  10.             /WEB-INF/classes/applicationContext.xml   
  11.             classpath:org/codehaus/xfire/spring/xfire.xml   
  12.         </param-value>   
  13.     </context-param>   
  14.   
  15.     <listener>   
  16.         <listener-class>   
  17.             org.springframework.web.context.ContextLoaderListener   
  18.         </listener-class>   
  19.     </listener>   
  20.   
  21.     <listener>   
  22.         <listener-class>   
  23.             org.springframework.web.util.IntrospectorCleanupListener   
  24.         </listener-class>   
  25.     </listener>   
  26.   
  27.   <servlet>   
  28.     <description>This is the description of my J2EE component</description>   
  29.     <display-name>This is the display name of my J2EE component</display-name>   
  30.     <servlet-name>Test</servlet-name>   
  31.     <servlet-class>test.Test</servlet-class>   
  32.   </servlet>   
  33.        
  34.        
  35.     <servlet-mapping>   
  36.     <servlet-name>Test</servlet-name>   
  37.     <url-pattern>/servlet/Test</url-pattern>   
  38.   </servlet-mapping>   
  39.   
  40.     <welcome-file-list>   
  41.         <welcome-file>index.html</welcome-file>   
  42.     </welcome-file-list>   
  43. </web-app>   

测试的action  Test.java

java 代码
  1. package test;   
  2.   
  3. import java.io.IOException;   
  4. import javax.servlet.ServletException;   
  5. import javax.servlet.http.HttpServlet;   
  6. import javax.servlet.http.HttpServletRequest;   
  7. import javax.servlet.http.HttpServletResponse;   
  8. import org.springframework.web.context.WebApplicationContext;   
  9. import org.springframework.web.context.support.WebApplicationContextUtils;   
  10.   
  11. public class Test extends HttpServlet {   
  12.   
  13.     /**  
  14.      * Constructor of the object.  
  15.      */  
  16.     public Test() {   
  17.         super();   
  18.     }   
  19.   
  20.     /**  
  21.      * Destruction of the servlet. <br>  
  22.      */  
  23.     public void destroy() {   
  24.         super.destroy(); // Just puts "destroy" string in log   
  25.         // Put your code here   
  26.     }   
  27.   
  28.     /**  
  29.      * The doGet method of the servlet. <br>  
  30.      *  
  31.      * This method is called when a form has its tag value method equals to get.  
  32.      *   
  33.      * @param request the request send by the client to the server  
  34.      * @param response the response send by the server to the client  
  35.      * @throws ServletException if an error occurred  
  36.      * @throws IOException if an error occurred  
  37.      */  
  38.     public void doGet(HttpServletRequest request, HttpServletResponse response)   
  39.             throws ServletException, IOException {   
  40.   
  41.         doPost(request, response);   
  42.     }   
  43.   
  44.     /**  
  45.      * The doPost method of the servlet. <br>  
  46.      *  
  47.      * This method is called when a form has its tag value method equals to post.  
  48.      *   
  49.      * @param request the request send by the client to the server  
  50.      * @param response the response send by the server to the client  
  51.      * @throws ServletException if an error occurred  
  52.      * @throws IOException if an error occurred  
  53.      */  
  54.     public void doPost(HttpServletRequest request, HttpServletResponse response)   
  55.             throws ServletException, IOException {   
  56.   
  57.         WebApplicationContext wc = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());   
  58.         IHello hello = (IHello)wc.getBean("testBean");   
  59.            
  60.         System.out.println(hello.helloTo("wcq"));   
  61.            
  62.     }   
  63.   
  64.     /**  
  65.      * Initialization of the servlet. <br>  
  66.      *  
  67.      * @throws ServletException if an error occure  
  68.      */  
  69.     public void init() throws ServletException {   
  70.         // Put your code here   
  71.     }   
  72.   
  73. }   
包太大,上传不了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值