spring + hibernate 实现 webService

好久没用过spring 了,今天项目需要用到还不会整合了,搞了一晚上终于可以了。写下来以后用


web.xml 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  	<!-- Spring3.2.2配置开始 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>
	<!-- Spring3.2.2配置结束 -->	
 	
 	<!-- cxf配置开始 -->
	<servlet>
		<servlet-name>CXFService</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>CXFService</servlet-name>
		<url-pattern>/webService/*</url-pattern>
	</servlet-mapping>
	<!-- cxf配置结束 -->
	
   <welcome-file-list>
     <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


jdbc.properties 配置

# JDBC
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.username=ljkj_app
jdbc.password=ljkj_app
jdbc.url=jdbc:oracle:thin:@192.168.0.8:1521:ljkj
jdbc.validationQuery=select 1 from dual
jdbc.maxpoolsize=30
jdbc.minpoolsize=2
jdbc.initialpoolsize=10
jdbc.maxidletime=20000


spring 配置
<?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:jaxws="http://cxf.apache.org/jaxws"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
	default-lazy-init="true" default-autowire="byName">
	
	 <!-- 自动加载Ben -->
	 <context:component-scan   base-package="com.legentec" annotation-config="true" />  
     

	  <!-- cxf 开始 --> 
	  <import resource="classpath:META-INF/cxf/cxf.xml" /> 
	  <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> 
	  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 
	  
	  <!-- ID 唯一标识、serviceClass 路径 、address webServices 路径(http) -->
	  <jaxws:server id="webServices" serviceClass="com.legentec.netbar.webservices.Service" address="/service">
	  		<jaxws:serviceBean>
				<ref bean="serviceImpl" />
			</jaxws:serviceBean>
	  </jaxws:server>
	  <!-- cxf 结束  --> 
	 
	<!-- 数据库配置信息  -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	<!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="mappingResources"> 
           <list> 
              <value>com/legentec/netbar/object/User.hbm.xml</value> 
           </list> 
        </property>
	  	<property name="dataSource"> 
            <ref bean="dataSource"/> 
        </property>  
        <property name="hibernateProperties">
			<props>
				<prop key="hibernate.show_sql">true</prop>
				<!-- 
					validate 加载hibernate时,验证数据库的结构
					update  加载hibernate时,检查数据库,如果表不存在,则创建,如果存在,则更新
					create  每次加载hiberante,都会创建表
					create-drop  每次加载hiberante,创建,卸载hiberante时,销毁
				-->
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>     
    </bean>
    <bean id="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass" value="${jdbc.driverClassName}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="maxPoolSize" value="${jdbc.maxpoolsize}"></property>
		<property name="minPoolSize" value="${jdbc.minpoolsize}"></property>
		<property name="initialPoolSize" value="${jdbc.initialpoolsize}"></property>
		<property name="maxIdleTime" value="${jdbc.maxidletime}"></property>
		<property name="testConnectionOnCheckin" value="true" > </property> 
    	<property name="idleConnectionTestPeriod" value="60" >  </property> 
   		<property name="preferredTestQuery" value="${jdbc.validationQuery}"></property> 
	</bean>
    	
    <bean id="userDAO" class="com.legentec.netbar.object.UserDAO"> 
        <property name="sessionFactory"> 
            <ref bean="sessionFactory"/> 
        </property> 
    </bean> 
	
</beans><span style="color:#666666;">	</span>

接口方法

package com.legentec.netbar.webservices;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface Service {
	String test(@WebParam(name="s") String s);
}<span style="color:#666666;">
</span>


实现方法

<span style="color:#333333;">package com.legentec.netbar.webservices.impl;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

import com.legentec.netbar.webservices.Service;

@WebService
@Component("serviceImpl")
public class ServiceImpl  implements Service{

	public String test(String s) {
		System.out.println("hello"+s);
		return "hello"+s;
	}
}</span><span style="color:#ff0000;">
</span>

项目结构



所需JAR 包  




http://localhost:7070/EWebServer/webService/service?wsdl




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值