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
这里主要是指定项目启动时,需要加载的配置文件及初始化类
spring与activit配置文件整合
配置主要集中在applicationContext.xml文件中。
主要有以下几点:
1、配置数据库数据源,这里以mysql为例
2、启用spring 的注解模式
3、配置事务
4、配置activiti的引擎,也即是activiti的主类。
5、配置activiti的各种服务类。
6、配置工具类,主要是避免调用过程中,重复调用和过多的new对象,这里我们通过spring来处理。
配置文件如下:
首先我们看一下 activiti所需要的包,
我们解压从官网下载的 activiti-5.17.0后,在wars文件夹下有两个example实例,这两个实例用所用的基本就是activiti所需的所有jar包了。
spring和mybatis的jar包,可以用war实例中提供的,也可以自己选择合适的版本下载,这是实例的源码,会在该系列教程结束后发放出来。
下面我们开始看配置文件:
首先是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_2_5.xsd"
- id="WebApp_ID" version="2.5">
- <display-name>itxxz</display-name>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 加载spring主配置文件 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath*:/applicationContext.xml
- </param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
- </listener>
- <!-- 配置spring拦截器 -->
- <servlet>
- <servlet-name>springMvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <!-- 配置请求路径 -->
- <param-value>classpath:spring-servlet-config.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup><!-- load-on-startup必须放在最后 -->
- </servlet>
- <servlet-mapping>
- <servlet-name>springMvc</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <!-- 验证码 -->
- <servlet>
- <servlet-name>authCode</servlet-name>
- <servlet-class>com.itxxz.servlet.AuthCodeServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>authCode</servlet-name>
- <url-pattern>/authCode</url-pattern>
- </servlet-mapping>
- <!-- 一缕转换为UTF-8编码 -->
- <filter>
- <filter-name>itxxzEncoding</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>itxxzEncoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- sesson超时时间 -->
- <session-config>
- <session-timeout>180</session-timeout>
- </session-config>
- <!-- 4040页面返回 -->
- <error-page>
- <error-code>404</error-code>
- <location>/WEB-INF/error.jsp</location>
- </error-page>
- <welcome-file-list>
- <welcome-file>/WEB-INF/pages/login.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
spring与activit配置文件整合
配置主要集中在applicationContext.xml文件中。
主要有以下几点:
1、配置数据库数据源,这里以mysql为例
2、启用spring 的注解模式
3、配置事务
4、配置activiti的引擎,也即是activiti的主类。
5、配置activiti的各种服务类。
6、配置工具类,主要是避免调用过程中,重复调用和过多的new对象,这里我们通过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:context="http://www.springframework.org/schema/context"
- 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-3.1.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
- <property name="driverClass">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <property name="jdbcUrl">
- <value>jdbc:mysql://127.0.0.1:3306/itxxz?useUnicode=true&characterEncoding=utf-8</value>
- </property>
- <property name="user">
- <value>root</value>
- </property>
- <property name="password">
- <value>root</value>
- </property>
- <property name="minPoolSize">
- <value>10</value>
- </property>
- <property name="maxPoolSize">
- <value>500</value>
- </property>
- <property name="initialPoolSize">
- <value>10</value>
- </property>
- <property name="maxIdleTime">
- <value>25000</value>
- </property>
- <property name="acquireIncrement">
- <value>5</value>
- </property>
- <property name="acquireRetryAttempts">
- <value>30</value>
- </property>
- <property name="acquireRetryDelay">
- <value>1000</value>
- </property>
- <property name="testConnectionOnCheckin">
- <value>false</value>
- </property>
- <property name="automaticTestTable">
- <value>t_c3p0</value>
- </property>
- <property name="idleConnectionTestPeriod">
- <value>18000</value>
- </property>
- <property name="checkoutTimeout">
- <value>5000</value>
- </property>
- </bean>
- <!-- 配置sqlSessionFactory -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"></property>
- <property name="configLocation" value="classpath:mybatis-config.xml"></property>
- </bean>
- <!-- 配置事务 -->
- <bean id="txManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <!--使用基于注解方式配置事务 -->
- <tx:annotation-driven transaction-manager="txManager" />
- <!-- 开启自动扫描 -->
- <context:annotation-config/>
- <context:component-scan base-package="com.itxxz" use-default-filters="false">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Component" />
- </context:component-scan>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.itxxz" />
- </bean>
- <!-- activiti事物管理 -->
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!-- 加载activiti引擎 -->
- <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
- <property name="processEngineConfiguration" ref="processEngineConfiguration" />
- </bean>
- <bean id="processEngineConfiguration"
- class="org.activiti.spring.SpringProcessEngineConfiguration">
- <property name="dataSource" ref="dataSource" />
- <property name="transactionManager" ref="transactionManager" />
- <property name="databaseSchemaUpdate" value="true" />
- <property name="jobExecutorActivate" value="false" />
- </bean>
- <!-- activiti的各种服务接口 -->
- <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
- <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
- <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
- <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
- <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
- <!--配置流程中工具类-->
- <bean id="commonUtil" class="com.itxxz.workflow.util.CommonUtil">
- </bean>
- </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
- <context:component-scan base-package="com.itxxz.*.web,com.itxxz.sys.*.web,com.jfok.server" use-default-filters="false">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
- </context:component-scan>
- <!-- 配置静态资源 -->
- <mvc:resources mapping="/images/**" location="/images/" />
- <mvc:resources mapping="/css/**" location="/css/" />
- <mvc:resources mapping="/fonts/**" location="/fonts/" />
- <mvc:resources mapping="/upload/**" location="/upload/" />
- <!-- 使用jsp作为视图 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass">
- <value>org.springframework.web.servlet.view.JstlView</value>
- </property>
- <property name="prefix" value="/WEB-INF/pages/"></property>
- <property name="suffix" value=".jsp"></property>
- </bean>
配置完成后,我们就可以通过tomcat启动来测试一下,如下图便是成功启动了。