將cxf與spring相關的架包拷到lib目錄下,然后在classpath下新建一個cxfbeans.xml(進行cxf與spring的集成)文件和applicationContext.xml(進行ssh2的配置),
applicationContext.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 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.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 配置數據源,導入c3p0-0.9.1.2.jar,mysql-connector-java-5.1.7-bin.jar --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"> <value>net.sourceforge.jtds.jdbc.Driver</value> </property> <property name="jdbcUrl"> <value>jdbc:jtds:sqlserver://localhost:9433/web_exam</value> </property> <property name="user"> <value>sa</value> </property> <property name="password"> <value>123</value> </property><!-- 初始化時獲取的連接數,取值應在minPoolSize與maxPoolSize之間。Default: 3 --><property name="initialPoolSize" value="1"/><!-- 連接池中保留的最小連接數。 --><property name="minPoolSize" value="1"/><!-- 連接池中保留的最大連接數。Default: 15 --><property name="maxPoolSize" value="300"/><!-- 最大空閑時間,60秒內未使用則連接被丟棄。若為0則永不丟棄。Default: 0 --><property name="maxIdleTime" value="60"/><!-- 當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3 --><property name="acquireIncrement" value="5"/><!-- 每60秒檢查所有連接池中的空閑連接。Default: 0 --><property name="idleConnectionTestPeriod" value="60"/><!-- C3P0連接池設定,hibernate自帶的連接池性能不行,而去使用第三方軟件,如C3P0 連接池的最小連接數 <property name="hibernate.c3p0.min_size" value = "5"></property> 最大連接數 <property name="hibernate.c3p0.max_size" value = "30"></property> 連接超時時間 <property name="hibernate.c3p0.timeout" value = "1800"></property> statemnets緩存大小 <property name="hibernate.c3p0.max_statements" value = "100"></property> 每隔多少秒檢測連接是否可正常使用 ;最后在網上找到一個辦法。為hibernate配置連接池,推薦用c3p0,然后配置c3p0的反空閑設置 idle_test_period,(只要小於MySQL的wait timeout即可,這句話后經證實不一定)。 <property name="hibernate.c3p0.idle_test_period" value = "121"></property> 當池中的連接耗盡的時候,一次性增加的連接數量,默認為3 <property name="hibernate.c3p0.acquire_increment" value = "1"></property> <property name="hibernate.c3p0.validate" value = "true"></property> --></bean> <bean name="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="properties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> <!--<prop key="hibernate.hbm2ddl.auto">update</prop> --><prop key="hibernate.cache.provider_class"> org.hibernate.cache.NoCacheProvider </prop> <!--<prop key="hibernate.show_sql">true</prop> --></props> </property> </bean> <!--定義了Hibernate的SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties" ref="hibernateProperties" /> <property name="mappingLocations"> <list> <value>classpath*:xidian/sl/entity/*.hbm.xml</value> </list> </property> </bean> <!-- 事務配置 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <!-- 事務攔截器bean需要依賴注入一個事務管理器 --> <property name="transactionManager" ref="transactionManager"/> <property name="transactionAttributes"> <!-- 下面配置事務屬性--> <props> <!--PROPAGATION_REQUIRE規則表示:在bean中所有以get開頭的方法,當拋出異 常時,自動回滾,並只讀,其他異常自動回滾--> <!-- 事務處理,如果沒有跟這里匹配的名,系統會默認是readOnly --> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="*">ISOLATION_SERIALIZABLE</prop> </props> </property> </bean> <!-- 定義BeanNameAutoProxyCreator--> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <!-- 指定對滿足哪些bean name的bean自動生成業務代理 --> <property name="beanNames"> <!-- 下面是所有需要自動創建事務代理的bean--> <list> <value>*Service</value> <value>*DAO</value> </list> <!-- 此處可增加其他需要自動創建事務代理的bean--> </property> <!-- 下面定義BeanNameAutoProxyCreator所需的事務攔截器--> <property name="interceptorNames"> <list> <!-- 此處可增加其他新的Interceptor --> <value>transactionInterceptor</value> </list> </property> </bean> <!-- 事務配置結束 --> </beans>
cxfbeans.xml
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <!-- 這些xml文件在cxf-2.5.0.jar的META-INF目錄下--> <!--<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> 警告提示已經廢棄了cxf-extension-soap.xml文件--> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <!-- 這里配置服務接口,后面描述 id:指在spring配置的bean的ID. Implementor:指明具體的實現類. Address:指明這個web service的相對地址 --> <!-- 考生登錄接口配置 --> <!-- 這里service中DAo層的注入必須寫在該配置文件中,因為客戶端只能看到該配置文件 --> <bean id="examineeLoginServiceImpl" class="xidian.sl.service.impl.webService.ExamineeLoginServiceImpl" > <property name="examineeLoginDAO" ref="examineeLoginDAO"></property> </bean> <jaxws:endpoint id="examineeLoginService" implementor="#examineeLoginServiceImpl" address="/examineeLogin" /> <!-- 考試開始接口配置 --> <bean id="examStartServiceImpl" class="xidian.sl.service.impl.webService.ExamStartServiceImpl" > <property name="examStartDAO" ref="examStartDAO"></property> </bean> <jaxws:endpoint id="examStartService" implementor="#examStartServiceImpl" address="/examStart" /> <!-- 接受答案處理 --> <bean id="answerManageServiceImpl" class="xidian.sl.service.impl.webService.AnswerManageServiceImpl" > <property name="answerManageDAO" ref="answerManageDAO"></property> </bean> <jaxws:endpoint id="answerManageService" implementor="#answerManageServiceImpl" address="/answerManage" /> <!-- 開啟tomcat服務器 ,訪問http://localhost:8080/WebExam/services/zipTest?wsdl http://localhost:8080/WebExam是本項目的訪問地址 services是由於web.xml配置所得,zipTest是由於Spring配置文件中的address屬性所得 --> </beans>
要使得客戶端能夠正常的調用,我們還必須在web.xml下進行訪問地址的配置,即配置Servlet(org.apache.cxf.transport.servlet.CXFServlet)的訪問
web.xml:
<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>/services/*</url-pattern> </servlet-mapping>