activiti5.17.0整合spring+mybatis

http://www.itxxz.com/a/gaoji/2015/0122/activiti_spring_mybatis.html

上一篇中我们介绍了eclipse整合activiti插件的过程,今天螃蟹就讲解下activiti5.17.0与spring和mybatis的整合。

首先我们看一下
activiti所需要的包,

我们解压从官网下载的
activiti-5.17.0后,在wars文件夹下有两个example实例,这两个实例用所用的基本就是activiti所需的所有jar包了。 

spring和mybatis的jar包,可以用war实例中提供的,也可以自己选择合适的版本下载,这是实例的源码,会在该系列教程结束后发放出来。

下面我们开始看配置文件:

首先是web.xml

这里主要是指定项目启动时,需要加载的配置文件及初始化类


  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4.  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  7. id="WebApp_ID" version="2.5">  
  8.   <display-name>itxxz</display-name>  
  9.     
  10.     <listener>  
  11.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  12.     </listener>  
  13.     <!-- 加载spring主配置文件 -->  
  14.     <context-param>  
  15.         <param-name>contextConfigLocation</param-name>  
  16.         <param-value>  
  17.             classpath*:/applicationContext.xml  
  18.         </param-value>  
  19.     </context-param>  
  20.       
  21.     <listener>     
  22.         <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>     
  23.     </listener>  
  24.     <!-- 配置spring拦截器 -->  
  25.     <servlet>  
  26.         <servlet-name>springMvc</servlet-name>  
  27.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  28.         <init-param>  
  29.             <param-name>contextConfigLocation</param-name>  
  30.             <!-- 配置请求路径 -->  
  31.             <param-value>classpath:spring-servlet-config.xml</param-value>  
  32.         </init-param>  
  33.         <load-on-startup>1</load-on-startup><!-- load-on-startup必须放在最后 -->  
  34.     </servlet>  
  35.     <servlet-mapping>  
  36.         <servlet-name>springMvc</servlet-name>  
  37.         <url-pattern>/</url-pattern>  
  38.     </servlet-mapping>  
  39.     <!-- 验证码 -->  
  40.     <servlet>  
  41.         <servlet-name>authCode</servlet-name>  
  42.          <servlet-class>com.itxxz.servlet.AuthCodeServlet</servlet-class>  
  43.     </servlet>  
  44.     <servlet-mapping>  
  45.         <servlet-name>authCode</servlet-name>  
  46.         <url-pattern>/authCode</url-pattern>  
  47.     </servlet-mapping>      
  48.     <!-- 一缕转换为UTF-8编码 -->  
  49.     <filter>  
  50.         <filter-name>itxxzEncoding</filter-name>  
  51.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  52.         <init-param>  
  53.             <param-name>encoding</param-name>  
  54.             <param-value>UTF-8</param-value>  
  55.         </init-param>  
  56.         <init-param>  
  57.             <param-name>forceEncoding</param-name>  
  58.             <param-value>true</param-value>  
  59.         </init-param>  
  60.     </filter>  
  61.     <filter-mapping>  
  62.         <filter-name>itxxzEncoding</filter-name>  
  63.         <url-pattern>/*</url-pattern>  
  64.     </filter-mapping>  
  65.     <!-- sesson超时时间 -->  
  66.     <session-config>  
  67.         <session-timeout>180</session-timeout>  
  68.     </session-config>  
  69.     <!-- 4040页面返回 -->  
  70.     <error-page>  
  71.         <error-code>404</error-code>  
  72.         <location>/WEB-INF/error.jsp</location>  
  73.      </error-page>  
  74.   
  75.   <welcome-file-list>  
  76.     <welcome-file>/WEB-INF/pages/login.jsp</welcome-file>  
  77.   </welcome-file-list>  
  78. </web-app>  


spring与activit配置文件整合

配置主要集中在applicationContext.xml文件中。

主要有以下几点:

1、配置数据库数据源,这里以mysql为例

2、启用spring 的注解模式

3、配置事务

4、配置activiti的引擎,也即是activiti的主类。

5、配置activiti的各种服务类。

6、配置工具类,主要是避免调用过程中,重复调用和过多的new对象,这里我们通过spring来处理。


配置文件如下:

 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4. xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:aop="http://www.springframework.org/schema/aop" 
  6. xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans     
  8.     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
  9.     http://www.springframework.org/schema/context     
  10.     http://www.springframework.org/schema/context/spring-context-3.1.xsd    
  11.     http://www.springframework.org/schema/aop     
  12.     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd    
  13.     http://www.springframework.org/schema/tx     
  14.     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">  
  15.   
  16.   
  17.         <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
  18.             <property name="driverClass">   
  19.                      <value>com.mysql.jdbc.Driver</value>   
  20.             </property>   
  21.             <property name="jdbcUrl">   
  22.                  <value>jdbc:mysql://127.0.0.1:3306/itxxz?useUnicode=true&characterEncoding=utf-8</value>   
  23.             </property>   
  24.             <property name="user">   
  25.                  <value>root</value>   
  26.             </property>   
  27.             <property name="password">   
  28.                  <value>root</value>   
  29.             </property>   
  30.             <property name="minPoolSize">   
  31.                  <value>10</value>  
  32.             </property>  
  33.             <property name="maxPoolSize">   
  34.                  <value>500</value>   
  35.             </property>   
  36.             <property name="initialPoolSize">   
  37.                  <value>10</value>   
  38.             </property>   
  39.             <property name="maxIdleTime">   
  40.                  <value>25000</value>   
  41.             </property>   
  42.             <property name="acquireIncrement">   
  43.                  <value>5</value>   
  44.             </property>   
  45.             <property name="acquireRetryAttempts">   
  46.                  <value>30</value>   
  47.             </property>   
  48.             <property name="acquireRetryDelay">   
  49.                  <value>1000</value>   
  50.             </property>   
  51.             <property name="testConnectionOnCheckin">   
  52.                  <value>false</value>   
  53.             </property>  
  54.             <property name="automaticTestTable">   
  55.                  <value>t_c3p0</value>   
  56.             </property>   
  57.             <property name="idleConnectionTestPeriod">   
  58.                  <value>18000</value>   
  59.             </property>  
  60.            <property name="checkoutTimeout">   
  61.                  <value>5000</value>   
  62.             </property>   
  63.         </bean>  
  64.       
  65.         <!-- 配置sqlSessionFactory -->  
  66.         <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  67.             <property name="dataSource" ref="dataSource"></property>  
  68.             <property name="configLocation" value="classpath:mybatis-config.xml"></property>  
  69.         </bean>  
  70.           
  71.         <!-- 配置事务 -->  
  72.         <bean id="txManager" 
  73. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  74.             <property name="dataSource" ref="dataSource"></property>  
  75.         </bean>  
  76.           
  77.         <!--使用基于注解方式配置事务 -->  
  78.         <tx:annotation-driven transaction-manager="txManager" />  
  79.       
  80.         <!-- 开启自动扫描 -->  
  81.         <context:annotation-config/>   
  82.         <context:component-scan base-package="com.itxxz" use-default-filters="false">  
  83.             <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />  
  84.             <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />  
  85.             <context:include-filter type="annotation" expression="org.springframework.stereotype.Component" />  
  86.         </context:component-scan>  
  87.       
  88.         <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  89.             <property name="basePackage" value="com.itxxz" />  
  90.         </bean>  
  91.       
  92.         <!-- activiti事物管理 -->  
  93.        <bean id="transactionManager" 
  94. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  95.            <property name="dataSource" ref="dataSource" />  
  96.         </bean>  
  97.       
  98.         <!-- 加载activiti引擎 -->  
  99.         <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">  
  100.           <property name="processEngineConfiguration" ref="processEngineConfiguration" />  
  101.         </bean>  
  102.         <bean id="processEngineConfiguration"
  103.  class="org.activiti.spring.SpringProcessEngineConfiguration">  
  104.           <property name="dataSource" ref="dataSource" />  
  105.           <property name="transactionManager" ref="transactionManager" />  
  106.           <property name="databaseSchemaUpdate" value="true" />  
  107.           <property name="jobExecutorActivate" value="false" />  
  108.         </bean>  
  109.       
  110.         <!-- activiti的各种服务接口 -->  
  111.         <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />  
  112.         <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />  
  113.         <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />  
  114.         <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />  
  115.         <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />  
  116.       
  117.         <!--配置流程中工具类-->  
  118.         <bean id="commonUtil" class="com.itxxz.workflow.util.CommonUtil">  
  119.         </bean>  
  120. </beans>  

  

spring-servlet-config.xml

到这里基本上就完成大部分工作了,还有一个文件,就是spring-servlet-config.xml。

通常我们会在这里配置静态资源或者试图页面等,以下配置只是简单配置了三点内容:


1、配置注解的扫描路径,也就是指定哪些地方需要使用注解

2、配置了静态资源的加载路径,也就是说这些路径不需要通过spring的拦截,可以直接调用。

3、配置jsp页面的试图,我们可以为其指定命名空间,也就是jsp页面的前缀。举个列子,如果我们调用的是WEB-INF/pages/itxxz.jsp这个页面,在配置好前缀路径WEB-INF/pages后,我们直接返回itxxz.jsp,spring会自动给填充缺省的路径。

配置代码如下:
 itxxz.com 


  1. <context:component-scan base-package="com.itxxz.*.web,com.itxxz.sys.*.web,com.jfok.server" use-default-filters="false">    
  2.     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />  
  3. </context:component-scan>  
  4.        
  5. <!-- 配置静态资源  -->    
  6. <mvc:resources mapping="/images/**" location="/images/" />   
  7. <mvc:resources mapping="/css/**" location="/css/" />   
  8. <mvc:resources mapping="/fonts/**" location="/fonts/" />   
  9. <mvc:resources mapping="/upload/**" location="/upload/" />   
  10.   
  11. <!-- 使用jsp作为视图 -->  
  12. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  13.     <property name="viewClass">  
  14.         <value>org.springframework.web.servlet.view.JstlView</value>  
  15.     </property>  
  16.     <property name="prefix" value="/WEB-INF/pages/"></property>  
  17.     <property name="suffix" value=".jsp"></property>  
  18. </bean>



配置完成后,我们就可以通过tomcat启动来测试一下,如下图便是成功启动了。


猜你喜欢

转载自hunan.iteye.com/blog/2351343
今日推荐