如何在JBPM的Action中访问Spring中的JavaBean

如果你希望在JbpmAction中,访问到spring中的bean:

 

1、  spring中按照通常的方法来定义action,该注入什么bean 就注入什么bean,如:

<beans>
 <!-- Spring bean visible inside jBPM processed -->
 <bean id="jbpmAction" class="org.MyJbpmActionHandler" singleton="true">
   <property name="someProp" ref="anotherBean"/>
   ...
 </bean>
..
</beans>

 

2、  JBPM的流程定义中,使用JbpmHandlerProxy类即可

<?xml version="1.0" encoding="UTF-8"?>

 

<process-definition name="simpleWorkflow">

 <start-state>

  <transition to="myState">

  </transition>

 </start-state>

 

 <state name="myState">

  <transition to="end">

   <action name="myAction" config-type="bean"

      class="org.springmodules.workflow.jbpm31.JbpmHandlerProxy">

    <targetBean>jbpmAction</targetBean>

   </action>

  </transition>

 </state>

 

 <end-state name="end"/>

</process-definition>

 

 

下面的代码就是spring-modules中的JbpmHandlerProxy的代码,可以参考一下:

 

public class JbpmHandlerProxy implements ActionHandler, AssignmentHandler, DecisionHandler, TaskControllerHandler {

 

    private static final Log logger = LogFactory.getLog(JbpmHandlerProxy.class);

 

    private String factoryKey;

 

    /**

     * Spring beanName name.

     */

    private String targetBean;

 

    /**

     * @return Returns the beanName.

     */

    public String getTargetBean() {

       return targetBean;

    }

 

    /**

     * @param targetBean The beanName to set.

     */

    public void setTargetBean(String bean) {

       this.targetBean = bean;

    }

 

    /**

     * @return the bean factory key

     */

    public String getFactoryKey() {

       return factoryKey;

    }

 

    /**

     * @param factoryKey the bean factory key

     */

    public void setFactoryKey(String factoryKey) {

       this.factoryKey = factoryKey;

    }

 

    /**

     * Retrieves the bean factory.

     *

     * @return

     */

    protected BeanFactory retrieveBeanFactory() {

       BeanFactoryLocator factoryLocator = new JbpmFactoryLocator();

       BeanFactoryReference factory = factoryLocator.useBeanFactory(factoryKey);

       if (factory == null)

           throw new IllegalArgumentException("no beanFactory found under key=" + factoryKey);

 

       try {

           return factory.getFactory();

       }

       finally {

           factory.release();

       }

    }

 

    /**

     * Find the beanName inside the Spring container.

     *

     * @return

     */

    protected Object lookupBean(Class type) {

       return retrieveBeanFactory().getBean(getTargetBean(), type);

    }

 

    /**

     * @see org.jbpm.graph.node.DecisionHandler#decide(org.jbpm.graph.exe.ExecutionContext)

     */

    public String decide(ExecutionContext executionContext) throws Exception {

       DecisionHandler handler = (DecisionHandler) lookupBean(DecisionHandler.class);

       if (logger.isDebugEnabled())

           logger.debug("using Spring-managed decisionHandler=" + handler);

       return handler.decide(executionContext);

    }

 

    /**

     * @see org.jbpm.graph.def.ActionHandler#execute(org.jbpm.graph.exe.ExecutionContext)

     */

    public void execute(ExecutionContext executionContext) throws Exception {

       ActionHandler action = (ActionHandler) lookupBean(ActionHandler.class);

 

       if (logger.isDebugEnabled())

           logger.debug("using Spring-managed actionHandler=" + action);

 

       action.execute(executionContext);

    }

 

    /**

     * @see org.jbpm.taskmgmt.def.AssignmentHandler#assign(org.jbpm.taskmgmt.exe.Assignable,

     * org.jbpm.graph.exe.ExecutionContext)

     */

    public void assign(Assignable assignable, ExecutionContext executionContext) throws Exception {

       AssignmentHandler handler = (AssignmentHandler) lookupBean(AssignmentHandler.class);

       if (logger.isDebugEnabled())

           logger.debug("using Spring-managed assignmentHandler=" + handler);

 

       handler.assign(assignable, executionContext);

    }

 

    /*

     * (non-Javadoc)

     * @see org.jbpm.taskmgmt.def.TaskControllerHandler#initializeTaskVariables(org.jbpm.taskmgmt.exe.TaskInstance,

     * org.jbpm.context.exe.ContextInstance, org.jbpm.graph.exe.Token)

     */

    public void initializeTaskVariables(TaskInstance taskInstance, ContextInstance contextInstance, Token token) {

       TaskControllerHandler handler = (TaskControllerHandler) lookupBean(TaskControllerHandler.class);

       if (logger.isDebugEnabled())

           logger.debug("using Spring-managed taskControllerHandler=" + handler);

       handler.initializeTaskVariables(taskInstance, contextInstance, token);

    }

 

    /*

     * (non-Javadoc)

     * @see org.jbpm.taskmgmt.def.TaskControllerHandler#submitTaskVariables(org.jbpm.taskmgmt.exe.TaskInstance,

     * org.jbpm.context.exe.ContextInstance, org.jbpm.graph.exe.Token)

     */

    public void submitTaskVariables(TaskInstance taskInstance, ContextInstance contextInstance, Token token) {

       TaskControllerHandler handler = (TaskControllerHandler) lookupBean(TaskControllerHandler.class);

       if (logger.isDebugEnabled())

           logger.debug("using Spring-managed taskControllerHandler=" + handler);

       handler.submitTaskVariables(taskInstance, contextInstance, token);

    }

 

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值