将webService(CXF)与spring集成
将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>
希望多多交流,多多关注,共同成就梦想