`

记一次简单的开发流程.(CXF2.7.4整合Hibernate4.2.3 spring3.2.2)

 
阅读更多

很久没从头到尾做一个应用了...所以 该忘记的都忘了...不该忘的也忘得差不多了...

 

这边简单记录一下了....

内容很简单(过程却很麻烦啊 对于我这种菜鸟来说...)

CXF和SH的整合 

 

新建项目为动态web项目

 

增加一个source fold:resource用来放一些配置文件 

 

导入需要的CXF的包和SH的包 手工去重(知道用maven会好点..但...还木有学..)

 

新建数据库和表 使用hibernate tool生成对应的entity

 

写spring的配置文件 配置数据源和session工厂 

(关于hibernate4.X和spring的集成:http://www.iteye.com/topic/1120924

 

然后编码 测试 然后用spring的注解注入(用注解做测试的时候要JUNIT要加上spring-test的注解 这里不提了)

 

 

 

 

整了这么久 CXF2.7.4里自带了spring3.0.7的一些jar包 我用了hibernate4.X 支持的要spring3.1以上...

现在我这边的jar包包含了3.0.7和3.2.2的 奇怪的是我要移除任何一方都会出错...但是两个同时在就相安无事... ...哎...



 相安无事的状态 我试过移除一方 排错(移除3.0.7的一些错误是完整性问题 移除3.2.2的一些错误是bean重名) 无奈还原就好了...无解.. .. .. .. .. ..

 

 

然后进行一些编写。。。

然后终于可以使用了....如下:



 

 

CXF2.7.4的spring包保留 然后引入spring3.2.2的包(给hibernate4提供支持)

两个spring的配置l文件这么写:

关于CXF的:

<?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:jaxws="http://cxf.apache.org/jaxws"
	xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://cxf.apache.org/jaxws
	http://cxf.apache.org/schemas/jaxws.xsd
	http://cxf.apache.org/jaxrs
	http://cxf.apache.org/schemas/jaxrs.xsd">

	<!-- 导入 CXF 扩充XML标记库,用于在Spring启用WebService标记 -->
	<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" />

	<!-- CXF 提供的内置拦截器  后面没用..-->
	<bean id="inLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
	<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />

	<jaxws:server id="MerchantWs" address="/MerchantWs">
		<jaxws:serviceBean>
			<ref bean="MerchantWsImpl" />
		</jaxws:serviceBean>
		<jaxws:inInterceptors>
			<ref bean="inLoggingInterceptor" />
		</jaxws:inInterceptors>
		<jaxws:outInterceptors>
			<ref bean="outLoggingInterceptor" />
		</jaxws:outInterceptors>
	</jaxws:server>
	
	<bean id="MerchantWsImpl" class="org.cc.cxf.userinterface.impl.MerchantWsImpl">

	</bean>

</beans>

 

 关于hibernate和声明式事务管理的:

<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        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-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    <context:property-placeholder location="classpath*:*.properties"/>  
    
    <context:component-scan base-package="org.cc.cxf"/>
    
    <context:annotation-config />
    
    <!-- 定义数据源Bean,使用C3P0数据源实现-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <!-- 指定连接数据库的驱动-->
        <property name="driverClass" value="${driverClass}"/>
        <!-- 指定连接数据库的URL-->
        <property name="jdbcUrl" value="${jdbcUrl}"/>
        <!-- 指定连接数据库的用户名-->
        <property name="user" value="${user}"/>
        <!-- 指定连接数据库的密码-->
        <property name="password" value="${password}"/>
        <!-- 指定连接池中保留的最大连接数. Default:15-->
        <property name="maxPoolSize" value="${maxPoolSize}"/>
        <!-- 指定连接池中保留的最小连接数-->
        <property name="minPoolSize" value="${minPoolSize}"/>
        <!-- 指定连接池的初始化连接数  取值应在minPoolSize 与 maxPoolSize 之间.Default:3-->
        <property name="initialPoolSize" value="${initialPoolSize}"/>
        <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0-->
        <property name="maxIdleTime" value="${maxIdleTime}"/>
        <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3-->
        <property name="acquireIncrement" value="${acquireIncrement}"/>
        <!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0-->
        <property name="maxStatements" value="${maxStatements}"/>
        <!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->
        <property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}"/>
        <!-- 定义在从数据库获取新连接失败后重复尝试的次数。 Default:30 -->
        <property name="acquireRetryAttempts" value="${acquireRetryAttempts}"/>
        <!-- 获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。Default:false -->
        <property name="breakAfterAcquireFailure" value="${breakAfterAcquireFailure}"/>
        <!-- 银性能消耗大请只在需要的时候是哟个它。如果设为true,那么在每个connection提交的时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable等提升连接测试的性能。 Default:false-->
        <property name="testConnectionOnCheckout" value="${testConnectionOnCheckout}"/>
    </bean>    
    
        <!-- 定义session工厂 -->
   <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan">
      <list>
        <value>${packageToScan}</value>
      </list>
    </property>
     <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
             ${dialect}
            </prop>
            <prop key="hibernate.show_sql">${show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${auto}</prop>
            <prop key="current_session_context_class">${current_session_context_class}</prop> 
        </props>
    </property>
  </bean>
  
     
     <!-- 开启AOP监听 只对当前配置文件有效 -->
	<aop:aspectj-autoproxy expose-proxy="true"/>
	
	<!-- 开启注解事务 只对当前配置文件有效 -->
  	<tx:annotation-driven transaction-manager="txManager"/>

    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>
    
        <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="merge*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="put*" propagation="REQUIRED" />
            <tx:method name="use*" propagation="REQUIRED"/>
            <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />
            <tx:method name="count*" propagation="REQUIRED" read-only="true" />
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />
            <tx:method name="list*" propagation="REQUIRED" read-only="true" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <aop:config expose-proxy="true">
        <aop:pointcut id="txPointcut" expression="execution(* org.cc..userinterface..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

</beans>

 以上内容很多复制粘贴得到的 hibernate4声明式事务管理是上文中一个链接里的

 

web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>cxf-sh</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:*.xml
    </param-value>
  </context-param>
  <listener>
    <listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
  </listener>
  
  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

 

 

使用:

C:\Users\dell>wsdl2java -encoding gbk -p org.cc.cxf.client -d H:\webservice http://localhost:8080/cxf-sh/ws/MerchantWs?wsdl

 

package org.cc.cxf.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class Main {

	public static void main(String[] args) {
		JaxWsProxyFactoryBean jpf=new JaxWsProxyFactoryBean();
		jpf.setAddress("http://localhost:8080/cxf-sh/ws/MerchantWs?wsdl");
		jpf.setServiceClass(MerchantWs.class);
		MerchantWs mw=(MerchantWs) jpf.create();
		System.out.println(mw.countAll());

	}

}

 输出:

2013-7-17 4:11:03 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://userinterface.cxf.cc.org/}MerchantWsService from class org.cc.cxf.client.MerchantWs
3

 

 

  • 大小: 15.1 KB
  • 大小: 83.3 KB
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics