xfire+spring 整合

来源:互联网 发布:怪物猎人数据库 编辑:程序博客网 时间:2024/04/29 16:29

1.web.xml

 <!-- spring 配置 start -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   <!--xfire使用  classpath:org/codehaus/xfire/spring/xfire.xml-->
   classpath:org/codehaus/xfire/spring/xfire.xml
   /WEB-INF/classes/applicationContext.xml
  </param-value>
 </context-param>
 <context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>/WEB-INF/classes/log4j.properties</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <servlet>
         <servlet-name>XFireServlet</servlet-name>
         <!--
          说明:
          xfire: org.codehaus.xfire.transport.http.XFireConfigurableServlet
                   <init-param>
               <param-name>config</param-name>
               <param-value>webservice/xfire/services.xml</param-value>
            </init-param>
          与spring集成:org.codehaus.xfire.spring.XFireSpringServlet
          -->
         <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
    </servlet>
     <servlet-mapping>
         <servlet-name>XFireServlet</servlet-name>
         <url-pattern>/servlet/XFireServlet/*</url-pattern>
     </servlet-mapping>
     <servlet-mapping>
         <servlet-name>XFireServlet</servlet-name>
         <url-pattern>/services/*</url-pattern>
     </servlet-mapping>

2.spring的 applicationContext.xml 配置

<?xml version="1.0" encoding="UTF-8"?>

<!--
  - Application context definition for JPetStore's business layer.
  - Contains bean references to the transaction manager and to the DAOs in
  - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
  -->
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
 
  <import resource="webservice/xfire/services.xml"/>
</beans>

3.接口与实现类 放在 org.cwq.service包下
package org.cwq.service;
import java.util.List;
import java.util.Map;
import javax.jws.WebMethod;
public interface IPresence{
 public void subscribePresence(String watcherUri, String[] presentities);
 public String[] ss(String watcherUri);
 public Map deal2(Map map);
 public void deal();
 public User dealBean();
 public String test(String msg);
 public String testInt(int i);
 public String testDouble(double i);
 public String testBool(boolean i);
}

package org.cwq.service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.dom4j.DocumentException;
public class PresenceImpl implements IPresence {

 public void subscribePresence( String watcherUri , String[] presentities ) {
  //System.out.println("===================================remote !");
  //System.out.println("Service:===Thread: "+Thread.currentThread().getId()+"--"+Thread.currentThread().getName());
  String m="";
  for(String a :presentities ){
   m+=a+" ";
  }
  System.out.println("Service:===remote execue "+watcherUri+"  "+m);
  if ( (watcherUri == null) || (presentities == null) ) {
   System.out.println("watcherUri or presentities is null! "+watcherUri);
   return;
  }
 }
   public Map deal2( Map map ) {
   System.out.println(map);
    return map;
   }
   public void deal() {
    System.out.println("=============================================/n/n/n成功!!!!!!!!!!");
   
   }
   public String test( String msg ) {
    System.out.println("---------------------service="+msg);
    return "#############################  /treturn  "+msg;
   }
   public String testBool( boolean i ) {
    // TODO Auto-generated method stub
    return i+" ";
   }
   public String testDouble( double i ) {
    // TODO Auto-generated method stub
    return (i*2.3)+"";
   }
   public String testInt( int i ) {
    // TODO Auto-generated method stub
    return (i*2)+"";
   }
 public String[] ss(String watcherUri){
  return new String[]{watcherUri,"ggoogo!!"};
 }
 public User dealBean(){
  User user=new User();
  user.setAge(34);
  user.setUserId("232323");
  user.setUserName("2222222222");
  return user;
 }
}

package org.cwq.service;

import com.sun.xml.internal.txw2.annotation.XmlElement;


public class User {
 private String userId;
 private String userName;
 private int age;
   @XmlElement ("namespace=http://service.cwq.org")
   public String getUserId() {
    return userId;
   }
 
   public void setUserId( String userId ) {
    this.userId = userId;
   }
   @XmlElement ("namespace=http://service.cwq.org")
   public String getUserName() {
    return userName;
   }
 
   public void setUserName( String userName ) {
    this.userName = userName;
   }
   @XmlElement ("namespace=http://service.cwq.org")
   public int getAge() {
    return age;
   }
 s
   public void setAge( int age ) {
    this.age = age;
   }
}

4. services.xml配置,放在 webservice/xfire/services.xml(webservice跟classpath:一致)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">


 <bean id="Ipresence"
  class="org.codehaus.xfire.spring.remoting.XFireExporter">
  <property name="serviceFactory" ref="xfire.serviceFactory"></property>
  <property name="xfire" ref="xfire"></property>
  <property name="serviceBean" ref="helloserviceImp"></property>
  <property name="serviceClass" value="org.cwq.service.IPresence"></property>
  <property name="name" value="presence"></property>
 </bean>

 <bean id="helloserviceImp" class="org.cwq.service.PresenceImpl"></bean>
 
 <!--
 <service  xmlns="http://xfire.codehaus.org/config/1.0">
  <name>presence1</name>
  <serviceClass>org.cwq.service.IPresence</serviceClass>
  <implementationClass>org.cwq.service.PresenceImpl</implementationClass>
 </service>
  -->
</beans>

 

5.测试 客户端测试

 

package org.cwq.service;

 

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.Invocation;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.OperationInfo;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
public class TestWebService {

 public static void main( String[] args ) throws  Exception {
  String xml = "<ROOT><USER><INFO><MEMBER><TEL></TEL><NAME>tel_sdf</NAME><ATTR><ATTRVALUE>tvalue_sdf</ATTRVALUE><ATTRCODE>tcode_sdf</ATTRCODE></ATTR></MEMBER><TEL>tel_sdf</TEL><ADD>add_sdf</ADD></INFO><AGE>24</AGE><USERNAME>chenwq</USERNAME><USERID>100001</USERID></USER><USER><INFO><TEL>tel_aaa</TEL><ADD>add_aaa</ADD></INFO><AGE>23</AGE><USERNAME>chenbe</USERNAME><USERID>100002</USERID></USER></ROOT>";
  Object[]  rs;
    Client client = new Client(new URL("http://localhost:8080/app/services/presence?wsdl"));

   
    rs=client.invoke("testDouble", new Object[] {1 });
    System.out.println(rs[0]);
   
    rs=client.invoke("subscribePresence", new Object[] {"http://www.a.com",new String[]{"adddd","b"}});
    //System.out.println(rs[0]);
  //  rs=client.invoke("ss", new Object[] {"http://www.a.com"});
  //  System.out.println("--"+rs[0]);
  //  String[] t=(String[])rs[0];
 //   System.out.println(t[0]+"-"+t[1]);
 //   rs= client.invoke("test", new String[] { "3" });
//    System.out.println(rs[0]);

   // rs=client.invoke("testInt", new Object[] {2 });
  //  System.out.println(rs[0]);
  //   rs=client.invoke("testBool", new Object[] {false });
  //  System.out.println(rs[0]);
   
    rs=client.invoke("dealBean", new Object[] {});
    System.out.println(rs[0]);
   
    
 }
 

}

 

------------有任何指教 请联系QQ 609824777