OSWorkflow 与Spring2&Hibernate3的结合

经过一番思量,决定在项目中引入OSWorkflow,参阅了很多网上的帖子,Spring2的结合比较容易解决,只要在spring的bean定义时使用的类正确就行了。但是对于Hibernate3,尝试了很多方法都没有搞定,貌似要完全的结合还需要写一些代码。于是退而求其次,仅仅做到让OSWorkflow 使用Spring定义的datasource就行了。省去在JNDI上另外再定义一个datasource的麻烦,并且减少数据库连接的开销。

具体做法:
1. 声明一个Store类
public class EtSpringMySQLWorkflowStore extends MySQLWorkflowStore implements ApplicationContextAware {
	private static ApplicationContext applicationContext; 

	public EtSpringMySQLWorkflowStore() {
		
	}
	public void init(Map props) throws StoreException {
        super.init(props);
        String springDataSource = (String) props.get("spring.datasource");
        ds=(DataSource)getApplicationContext().getBean(springDataSource);
    }
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}
	public void setApplicationContext(ApplicationContext applicationContext) {
		EtSpringMySQLWorkflowStore.applicationContext = applicationContext;
	}
	
}


2. 在osworkflow.xml中如下定义store:
<persistence class="com.xxx.workflow.EtSpringMySQLWorkflowStore">
	<property key="spring.datasource" value="dataSource"/>  


3. OSWorkflow的使用与普通方式一样:
				// 创建工作流实例
				Map<String, Object> transientVars = new HashMap<String, Object>();
				transientVars.put(ServletContext.class.getName(),
						this.servletContext);
				Workflow wf = new BasicWorkflow("");
//				Workflow wf=(Workflow)getBean("workflow");
				try {
					long wfid = wf.initialize("exchange", 100, null);
					wf.getPropertySet(wfid).setString("modelName",getText("exchange"));
					wf.getPropertySet(wfid).setInt("modelId",
							model.getExchangeId().intValue());
					wf.doAction(wfid, 1, transientVars);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 

你可能感兴趣的:(spring,xml,bean,workflow,Exchange)