首页 > 代码库 > 【整合篇】JBPM4.4与Spring整合
【整合篇】JBPM4.4与Spring整合
我们大家都知道容器的好处,那么工作流也提供了与spring整合的方式,将工作流引擎由spring容器统一管理起来,共同拥有容器的特性。下面来从代码的角度来看看整合与不整合的对比:
未整合:
引入相应的jar包,使用hibernate来持久化
配置文件:
jbpm.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?> <jbpm-configuration> <import resource="jbpm.default.cfg.xml" /> <import resource="jbpm.businesscalendar.cfg.xml" /> <import resource="jbpm.tx.hibernate.cfg.xml" /> <import resource="jbpm.jpdl.cfg.xml" /> <import resource="jbpm.bpmn.cfg.xml" /> <import resource="jbpm.identity.cfg.xml" /> <!-- Job executor is excluded for running the example test cases. --> <!-- To enable timers and messages in production use, this should be included. --> <!-- <import resource="jbpm.jobexecutor.cfg.xml" /> --> </jbpm-configuration>
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/jbpm</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">hejingyuan</property> <!-- <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property> <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property> <property name="hibernate.connection.url">jdbc:hsqldb:mem:.</property> <property name="hibernate.connection.username">sa</property> <property name="hibernate.connection.password"></property> --> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.format_sql">true</property> <mapping resource="jbpm.repository.hbm.xml" /> <mapping resource="jbpm.execution.hbm.xml" /> <mapping resource="jbpm.history.hbm.xml" /> <mapping resource="jbpm.task.hbm.xml" /> <mapping resource="jbpm.identity.hbm.xml" /> </session-factory> </hibernate-configuration>
申请时应用:
ProcessEngine pe = Configuration.getProcessEngine(); TaskService ts = pe.getTaskService(); String taskId = request.getParameter("taskId"); String owner = request.getParameter("owner"); int day = Integer.parseInt(request.getParameter("day")); String reason = request.getParameter("reason"); Map map = new HashMap(); map.put("day", day); map.put("reason", reason); map.put("msg", "<hr>申请人:"+owner+"<br>请假天数:"+day+"<br>请假原因:"+reason); ts.setVariables(taskId,map); ts.completeTask(taskId); response.sendRedirect("index.jsp");
对应的源码:
/** singletone instance */ private static ProcessEngine singleton; /** get the singleton ProcessEngine that is created from the default * configuration file 'jbpm.cfg.xml'. */ public static ProcessEngine getProcessEngine() { if (singleton == null) { synchronized (Configuration.class) { if (singleton == null) { singleton = new Configuration().setResource("jbpm.cfg.xml").buildProcessEngine(); } } } return Configuration.singleton; }
在jbpm的jar包下,我们可以看到:
这些是jbpm.cfg.xml文件中引入的文件
如何识别的hibernate的配置文件:
整合后:
使用ssh架构:
web.xml文件:
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SSH</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置Spring的OpenSessionInViewFilter,以解决懒加载异常的问题 --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- hejingyuan添加 --> <filter> <filter-name>SSH</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>SSH</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- 结束 --> <!-- open session in view 的配置--> <!-- <filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> --> </web-app>
struts.xml配置文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!-- 问题一: namespace="/" 错误,找不到返回值 --> <!-- 配置为开发模式 --> <constant name="struts.devMode" value=http://www.mamicode.com/"true" />>sring的配置文件: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: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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 自动扫描与装配bean --> <context:component-scan base-package="com.hjy.ssh"></context:component-scan> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value=http://www.mamicode.com/"com.mysql.jdbc.Driver"> >
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?> <!-- <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">--> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <!-- <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1/ssh</property> <property name="connection.username">root</property> <property name="connection.password">hejingyuan</property> --> <!-- JDBC connection pool (use the built-in) --> <!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- Enable Hibernate's automatic session context management --> <!-- <property name="current_session_context_class">thread</property> --> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="com/hjy/ssh/beans/User.hbm.xml"/> <mapping resource="com/hjy/ssh/beans/Application.hbm.xml"/> <mapping resource="com/hjy/ssh/beans/ApproveInfo.hbm.xml"/> <!-- 导入JBPM4.4的映射文件 --> <mapping resource="jbpm.repository.hbm.xml" /> <mapping resource="jbpm.execution.hbm.xml" /> <mapping resource="jbpm.history.hbm.xml" /> <mapping resource="jbpm.task.hbm.xml" /> <mapping resource="jbpm.identity.hbm.xml" /> </session-factory> </hibernate-configuration>jbpm.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?> <jbpm-configuration> <import resource="jbpm.default.cfg.xml" /> <import resource="jbpm.businesscalendar.cfg.xml" /> <!-- <import resource="jbpm.tx.hibernate.cfg.xml" /> --> <!-- 与Spring整合需要导入jbpm.tx.spring.cfg.xml文件 --> <import resource="jbpm.tx.spring.cfg.xml" /> <import resource="jbpm.jpdl.cfg.xml" /> <import resource="jbpm.bpmn.cfg.xml" /> <import resource="jbpm.identity.cfg.xml" /> </jbpm-configuration>
部署应用:
@Resource protected ProcessDefinitionService processDefinitionService; /** 部署 ,即添加一个流程定义(上传png和xml文件)*/ public String add() throws Exception { ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(upload)); try { processDefinitionService.deploy(zipInputStream); } finally { zipInputStream.close(); } return "toList"; }实现类:
@Override public void deploy(ZipInputStream zipInputStream) { // 部署流程 processEngine.getRepositoryService()// .createDeployment()// .addResourcesFromZipInputStream(zipInputStream)// .deploy(); }总结:
以上通过代码的方式展示,主要是让大家更加系统性的来了解工作流的工作原理,而看配置文件也是最简单的一种方式。对于是否与spring或者其他框架整合,相信大家通过做日常项目也能体会到应用框架的优势,以上的对比只是让大家有个直观的认识。
【整合篇】JBPM4.4与Spring整合
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。