WebService开发实例 (Xfire+Spring+Hibernate)

 

请注意,需要下载Xfire 1.2.6 spring2.0,hibernate 3.0相关类库及相关数据库的jdbc驱动。本文相关内容是在myeclipse5.1下完成。

 

一、           首先在web.xml中添加对xfirespring支持的相关内容,如下:

 

<?xml version="1.0" encoding="ISO-8859-1"?>

 

<!DOCTYPE web-app

    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<!-- START SNIPPET: xfire -->

<context-param>

<!xfirespring相关配置文件位置 -->

 

        <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/applicationContext.xml 

        classpath:org/codehaus/xfire/spring/xfire.xml</param-value>

    </context-param>

 

    <context-param>

        <param-name>log4jConfigLocation</param-name>

        <param-value>/WEB-INF/log4j.properties</param-value>

    </context-param>

   

    <context-param>

        <param-name>webAppRootKey</param-name>

        <param-value>webservicetest.root</param-value>

    </context-param>

 

 

     <filter>

        <filter-name>sessionFilter</filter-name>

        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

    </filter>  

    <filter-mapping>

        <filter-name>sessionFilter</filter-name>

        <url-pattern>/services/*</url-pattern>

    </filter-mapping>       

      

    <listener>

        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

    </listener>

 

    <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>/services/*</url-pattern>

    </servlet-mapping>      

   

<!-- END SNIPPET: xfire -->

</web-app>

 

二、           applicationContext.xml文件内容:

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

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

 

<!-- 数据库连接配置 -->

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

   

        <property name="location" value="/WEB-INF/jdbc_connect.properties"/>

       

    </bean>

       

    <!-- C3P0连接池配置 -->

   

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">    

       

        <property name="driverClass" value="${jdbc.driverClassName}"/>

        <property name="jdbcUrl" value="${jdbc.url}"/>

        <property name="user" value="${jdbc.username}"/>

        <property name="password" value="${jdbc.password}"/>

       

        <property name="initialPoolSize">

            <value>5</value>

        </property>

         <property name="minPoolSize">

             <value>5</value>

         </property>

         <property name="maxPoolSize">

             <value>10</value>

         </property>

         <property name="acquireIncrement">

             <value>2</value>

         </property>

         <property name="maxIdleTime">

             <value>60</value>

         </property>

         <property name="maxStatements">

             <value>0</value>

         </property>    

    </bean>

    <!-- C3P0结束 -->

   

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <property name="dataSource" ref="dataSource"/>

        <property name="mappingResources">

            <list>

            <!hibernatehbm文件位置 ,至于hbm文件可以通过工具生成,这里就不详述了-->

                <value>com/dao/emp.hbm.xml</value>             

            </list>

        </property>

        <property name="hibernateProperties">

            <props>

                <prop key="hibernate.dialect">${hibernate.dialect}</prop>      

                <prop key="hibernate.show_sql">true</prop>                 

                <prop key="hibernate.jdbc.fetch_size">100</prop>

                <prop key="hibernate.jdbc.batch_size">50</prop>

                <prop key="hibernate.use_outer_join">true</prop>               

                <prop key="hibernate.connection.SetBigStringTryClob">true</prop>

            </props>           

        </property>

       

        <property name="eventListeners">

            <map>

                <entry key="merge">

                    <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>

                </entry>

            </map>

        </property>

   

    </bean>

   

   

   

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

        <property name="sessionFactory" ref="sessionFactory"/>

    </bean>

   

    <bean id="QueryHib" class="Hello.HelloworldImpl ">

        <property name="sessionFactory" ref="sessionFactory"/>

    </bean>

     

   

    <!webservice bean -->

   

    <bean id="QueryHibRis" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

        <property name="transactionManager" ref="transactionManager"/>

        <property name="target" ref="QueryHib"/>

        <property name="transactionAttributes">

            <props>        

            <prop key="*">PROPAGATION_REQUIRED</prop>                  

            </props>

        </property>

    </bean>

   

</beans>

 

<!-- jdbc_connect.properties文件内容,主要是数据库配置,以下是oracle数据库 -->

 

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver

jdbc.url=jdbc:oracle:thin:@172.17.99.230:1521:bsrun

jdbc.username=user

jdbc.password=user

hibernate.dialect=org.hibernate.dialect.Oracle9Dialect

 

三、           xfire-servlet.xml文件内容,如下:

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

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"  >

 

<beans>

    <!-- START SNIPPET: xfire -->

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

        <property name="urlMap">

            <map>

                <entry key="/QueryService">

                    <ref bean="query"/>

                </entry>   

             </map>

        </property>

       

    </bean>

   

    <!-- Declare a parent bean with all properties common to both services -->

    <bean id="query" 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="QueryHibRis"/>  <!请注意这个需在applicationContext.xml有定义-->

 

        </property>

        <property name="serviceClass">

            <value> Hello.IHelloworld </value>

        </property>

       

    </bean>  

    <!-- END SNIPPET: xfire -->

</beans>

 

四、Hello.HelloworldImpl类内容,主要是查询数据库并返回结果

 

package Hello;

 

 

import java.util.Collection;

 

//Generated by MyEclipse

 

public class HelloworldImpl implements IHelloworld {

   

    public Collection User(){  

    return getHibernateTemplate().find("select new Helloworld.User(userid,username) from emp"); 

               

    }

   

}

 

IHelloworld接口内容

package Hello;

//Generated by MyEclipse

 

import java.util.Collection;

 

 

public interface IHelloworld {

   

    public Collection User();

}

//xfire对于返回collection的,需要定义接口的IHelloworld.aegis.xml文件,需放在当前接口文件目录下。

 

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

 <mappings>

    <mapping>

        <method name= "User" >

            <return-type componentType= "Helloworld.User" />          

        </method>         

       

    </mapping>   

   

</mappings>

 

Helloworld.User类内容:

package Helloworld;

 

public class User {

   

    String userid;

    String username;

//一定要定义此构造函数,在Hello.HelloworldImpl中有用到哟

    Public User(String userid,String username){

        this.userid = userid;

this.username = username;

 

}  

 

    public String getUserid() {

        return userid;

    }

    public void setUserid(String userid) {

        this.userid = userid;

    }

    public String getUsername() {

        return username;

    }

    public void setUsername(String username) {

        this.username = username;

    }

 

}

 

完结,以上代码是我在实际应用中改编而成,都经过测试,肯定好用。

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
package com.xfire.core.client; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import com.xfire.core.entity.UserInfo; import com.xfire.core.service.IUserInfoService; /** *@author jilongliang *@Date 2012-3-5 * */ public class UserInfoClient { public static void main(String[] args) { getServiceList(); setServiceList(); } static String url = "http://localhost:8081/xfire/services/UserInfo"; /** * */ public static void getServiceList() { Service service = new ObjectServiceFactory() .create(IUserInfoService.class); try { IUserInfoService iAddressService = (IUserInfoService) new XFireProxyFactory() .create(service, url); List list = (ArrayList) iAddressService .getAddressList(); System.out.println("一共多少条数据:" + list.size()); for (Iterator iter = list.iterator(); iter.hasNext();) { UserInfo a = iter.next(); System.out.println(a); } } catch (MalformedURLException e) { e.printStackTrace(); } } public static void setServiceList() { Service service = new ObjectServiceFactory() .create(IUserInfoService.class); try { IUserInfoService iAddressService = (IUserInfoService) new XFireProxyFactory() .create(service, url); List listAdd = new ArrayList(); UserInfo address = new UserInfo(); address.setIdentifier(1); address.setCountry("中國"); address.setProivice("廣東省"); address.setCity("陽江"); address.setAddress("廣東陽春"); address.setPostCode("1111111"); address.setExist(false); address.setArrary(new String[] { "22", "23", "24" }); listAdd.add(address); address.setIdentifier(2); address.setCountry("中國"); address.setProivice("廣東省"); address.setCity("陽江"); address.setAddress(

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值