如果你希望在Jbpm的Action中,访问到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); }
} |