spring+hibernate+osworkflow组合的一点改进

接上例:http://blog.csdn.net/kunshan_shenbin/archive/2008/10/23/3128756.aspx

上例中采取了在spring配置文件中定义workflow的caller的做法非常的不灵活。

考虑到这个原因,决定新建一个WebBasicWorkflow类。

代码如下:

WebWorkflow.java

  1. package tw.idv.idealist.base;
  2. import com.opensymphony.workflow.Workflow;
  3. public interface WebWorkflow extends Workflow {
  4.     public abstract void setCaller(String caller);
  5. }

WebBasicWorkflow.java

  1. package tw.idv.idealist.base;
  2. import com.opensymphony.workflow.AbstractWorkflow;
  3. import com.opensymphony.workflow.basic.BasicWorkflowContext;
  4. public class WebBasicWorkflow extends AbstractWorkflow implements WebWorkflow {
  5.     public WebBasicWorkflow(){
  6.     }
  7.     
  8.     public WebBasicWorkflow(String caller){
  9.         
  10.         super.context = new BasicWorkflowContext(caller);
  11.     }
  12.     
  13.     public void setCaller(String caller) {
  14.         /*
  15.         if(caller == null)
  16.             throw new IllegalArgumentException("Argument caller is Null!");
  17.         if(super.context == null)
  18.             super.context = new BasicWorkflowContext(caller);
  19.         else
  20.             throw new IllegalArgumentException((new StringBuilder()).append("the workflow ").append(toString()).append(" has already init the Context.").toString());
  21.         */
  22.         super.context = new BasicWorkflowContext(caller);
  23.     }
  24. }

修改如下几个文件:

applicationContext.xml

  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:aop="http://www.springframework.org/schema/aop"
  5.     xmlns:tx="http://www.springframework.org/schema/tx"
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  9.     <bean id="propertyConfigurer"
  10.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  11.         <property name="location">
  12.             <value>/WEB-INF/jdbc.properties</value>
  13.         </property>
  14.     </bean>
  15.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  16.         <property name="driverClassName">
  17.             <value>${jdbc.driverClassName}</value>
  18.         </property>
  19.         <property name="url">
  20.             <value>${jdbc.url}</value>
  21.         </property>
  22.         <property name="username">
  23.             <value>${jdbc.username}</value>
  24.         </property>
  25.         <property name="password">
  26.             <value>${jdbc.password}</value>
  27.         </property>
  28.         <property name="maxWait">
  29.             <value>${jdbc.maxWait}</value>
  30.         </property>
  31.         <property name="removeAbandoned">
  32.             <value>${jdbc.removeAbandoned}</value>
  33.         </property>
  34.         <property name="removeAbandonedTimeout">
  35.             <value>${jdbc.removeAbandonedTimeout}</value>
  36.         </property>
  37.         <property name="logAbandoned">
  38.             <value>${jdbc.logAbandoned}</value>
  39.         </property>
  40.     </bean>
  41.     
  42.     <bean id="sessionFactory"
  43.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  44.         <property name="dataSource">
  45.             <ref bean="dataSource" />
  46.         </property>
  47.         <property name="mappingResources">
  48.             <list>
  49.                 <value>com/opensymphony/workflow/spi/hibernate/HibernateCurrentStep.hbm.xml</value>
  50.                 <value>com/opensymphony/workflow/spi/hibernate/HibernateHistoryStep.hbm.xml</value>
  51.                 <value>com/opensymphony/workflow/spi/hibernate/HibernateWorkflowEntry.hbm.xml</value>
  52.             </list>
  53.         </property>
  54.         <property name="hibernateProperties">
  55.             <props>
  56.                 <prop key="hibernate.dialect">
  57.                     ${hibernate.dialect}
  58.                 </prop>
  59.                 <prop key="hibernate.show_sql">
  60.                     ${hibernate.show_sql}
  61.                 </prop>
  62.                 <prop key="hibernate.jdbc.batch_size">
  63.                     ${hibernate.jdbc.batch_size}
  64.                 </prop>
  65.                 <prop key="hibernate.generate_statistics">
  66.                     ${hibernate.generate_statistics}
  67.                 </prop>
  68.                 <prop key="hibernate.hbm2ddl.auto">
  69.                     ${hibernate.hbm2ddl.auto}
  70.                 </prop>
  71.             </props>
  72.         </property>
  73.         <property name="eventListeners">
  74.             <map>
  75.                 <entry key="merge">
  76.                     <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
  77.                 </entry>
  78.             </map>
  79.         </property>
  80.     </bean>
  81.   
  82.     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  83.         <property name="sessionFactory">
  84.             <ref local="sessionFactory" />
  85.         </property>
  86.     </bean>
  87.     <bean id="workflowStore" class="com.opensymphony.workflow.spi.hibernate3.SpringHibernateWorkflowStore">
  88.         <property name="sessionFactory"><ref local="sessionFactory"/></property>
  89.         <property name="propertySetDelegate">
  90.             <bean id="propertySetDelegate" class="com.opensymphony.workflow.util.PropertySetDelegateImpl"/>
  91.         </property>
  92.     </bean>
  93.     
  94.     <bean id="workflowTypeResolver" class="com.opensymphony.workflow.util.SpringTypeResolver"/>
  95.     
  96.     <bean id="workflowFactory" class="com.opensymphony.workflow.spi.hibernate.SpringWorkflowFactory" init-method="init">
  97.         <property name="resource"><value>workflows.xml</value></property>
  98.         <property name="reload"><value>false</value></property>
  99.     </bean>
  100.     
  101.     <bean id="osworkflowConfiguration" class="com.opensymphony.workflow.config.SpringConfiguration">
  102.         <property name="store"><ref local="workflowStore"/></property>
  103.         <property name="factory"><ref local="workflowFactory"/></property>      
  104.     </bean>
  105.     
  106.     <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
  107.         <property name="transactionManager"><ref local="transactionManager"/></property>
  108.         <property name="transactionAttributes">
  109.             <props>
  110.                 <prop key="*">PROPAGATION_REQUIRED</prop>
  111.             </props>
  112.         </property>
  113.     </bean>
  114. <!--
  115.     <bean id="workflow" class="org.springframework.aop.framework.ProxyFactoryBean">
  116.         <property name="proxyInterfaces">
  117.             <value>com.opensymphony.workflow.Workflow</value>
  118.         </property>
  119.         <property name="interceptorNames">
  120.             <list>
  121.                 <value>transactionInterceptor</value>
  122.                 <value>workflowTarget</value>
  123.             </list>
  124.         </property>
  125.     </bean>
  126.     <bean id="workflowTarget" class="com.opensymphony.workflow.basic.BasicWorkflow">
  127.         <constructor-arg><value>shenbin</value></constructor-arg>
  128.         <property name="configuration"><ref local="osworkflowConfiguration"/></property>    
  129.     </bean>
  130. -->
  131.     <bean id="workflow" class="org.springframework.aop.framework.ProxyFactoryBean">
  132.         <property name="proxyInterfaces">
  133.             <value>tw.idv.idealist.base.WebWorkflow</value>
  134.         </property>
  135.         <property name="interceptorNames">
  136.             <list>
  137.                 <value>transactionInterceptor</value>
  138.                 <value>workflowTarget</value>
  139.             </list>
  140.         </property>
  141.     </bean>
  142.     
  143.     <bean id="workflowTarget" class="tw.idv.idealist.base.WebBasicWorkflow" scope="prototype">
  144.         <property name="configuration"><ref local="osworkflowConfiguration"/></property>    
  145.     </bean>
  146. </beans>

LeaveAction.java

  1. package tw.idv.idealist.actions;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import org.springframework.web.context.WebApplicationContext;
  8. import org.springframework.web.context.support.WebApplicationContextUtils;
  9. import tw.idv.idealist.Leave;
  10. import tw.idv.idealist.base.WebWorkflow;
  11. public class LeaveAction extends HttpServlet {
  12.     private static final long serialVersionUID = 1376955339587565809L;
  13.     public void doGet(HttpServletRequest req, HttpServletResponse resp)
  14.             throws ServletException, IOException {
  15.         doPost(req, resp);
  16.     }
  17.     public void doPost(HttpServletRequest req, HttpServletResponse resp)
  18.             throws ServletException, IOException {
  19.         
  20.         WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
  21.         WebWorkflow workflow = (WebWorkflow)context.getBean("workflow");
  22.         Leave leave = new Leave();
  23.         String employee = req.getParameter("employee");
  24.         workflow.setCaller(employee);
  25.         long workflowId = leave.send(workflow);
  26.         resp.sendRedirect("allow.jsp?workflowId=" + workflowId);
  27.     }
  28. }

AllowAction.java

  1. package tw.idv.idealist.actions;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import org.springframework.web.context.WebApplicationContext;
  8. import org.springframework.web.context.support.WebApplicationContextUtils;
  9. import tw.idv.idealist.Leave;
  10. import tw.idv.idealist.base.WebWorkflow;
  11. public class AllowAction extends HttpServlet {
  12.     private static final long serialVersionUID = -8935624987243166064L;
  13.     public void doGet(HttpServletRequest req, HttpServletResponse resp)
  14.             throws ServletException, IOException {
  15.         doPost(req, resp);
  16.     }
  17.     public void doPost(HttpServletRequest req, HttpServletResponse resp)
  18.             throws ServletException, IOException {
  19.         
  20.         WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
  21.         WebWorkflow workflow = (WebWorkflow)context.getBean("workflow");
  22.         String workflowId = req.getParameter("workflowId");
  23.         String actionId = req.getParameter("actionId");
  24.         Leave leave = new Leave();
  25.         workflow.setCaller("manager");
  26.         leave.allow(workflow, Long.parseLong(workflowId), Integer.parseInt(actionId));
  27.         
  28.         resp.sendRedirect("completed.jsp");
  29.     }
  30. }

本来的做法其实来源于下面的文中:

http://read.newbooks.com.cn/info/197933.html

他里面包换的实现所在网址:

http://groups.google.com/group/ahxu/browse_thread/thread/2bf91654d871d7e4/5adfec0e7e83c351?#5adfec0e7e83c351

http://groups.google.com/group/ahxu/msg/c12d0f4a8950d22d

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值