(1)model pojo 加载(在sessionFactory里加载):
记得加包:spring-modules-jbpm31.jar
<bean id=“sessionFactory” …….>
……….
<property name="mappingLocations">
<list>
<value>classpath*:/org/jbpm/**/*.hbm.xml</value>
</list>
</property>
</bean>
(2)spring 管理的jbpm数据操作接口
<bean id=“jbpmConfiguration”
class="org.springmodules.workflow.jbpm31.LocalJbpmConfigurationFactoryBean">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="configuration" value="classpath:jbpm.cfg.xml"/>
// jbpm.cfg.xml 文件路径在src下面
</bean>
<bean id=“jbpmTemplate” class="org.springmodules.workflow.jbpm31.JbpmTemplate">
<constructor-arg index="0" ref="jbpmConfiguration" />
</bean>
jbpmTmepate 与spring的JdbcTemplate有很想似之处。
(3)配置过滤器 JbpmContextFilter
<filter>
<filter-name>JbpmContextFilter</filter-name>
<filter-class>
com.jh.jcs.workflow.filter.JbpmContextFilter
</filter-class>
</filter>
package com.jh.jcs.workflow.filter;
import java.io.IOException;
import java.io.Serializable;
import java.security.Principal;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springmodules.workflow.jbpm31.JbpmTemplate;
import org.unitils.spring.SpringUnitils;
public class JbpmContextFilter implements Filter, Serializable {
private static final long serialVersionUID = 1L;
String jbpmConfigurationResource = null;
String jbpmContextName = null;
boolean isAuthenticationEnabled = true;
FilterConfig config;
public void setFilterConfig(FilterConfig config) {
this.config = config;
}
public FilterConfig getFilterConfig() {
return config;
}
public void init(FilterConfig filterConfig) throws ServletException {
// get the jbpm configuration resource
this.jbpmConfigurationResource = filterConfig.getInitParameter("jbpm.configuration.resource");
// get the jbpm context to be used from the jbpm configuration
this.jbpmContextName = filterConfig.getInitParameter("jbpm.context.name");
if (jbpmContextName==null) {
jbpmContextName = JbpmContext.DEFAULT_JBPM_CONTEXT_NAME;
}
// see if authentication is turned off
String isAuthenticationEnabledText = filterConfig.getInitParameter("authentication");
if ( (isAuthenticationEnabledText!=null)
&& ("disabled".equalsIgnoreCase(isAuthenticationEnabledText))
) {
isAuthenticationEnabled = false;
}
this.setFilterConfig(filterConfig);
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
String actorId = null;
//通过filterconfig 得到servletcontext
ServletContext context = getFilterConfig().getServletContext();
// see if we can get the authenticated swimlaneActorId
if (servletRequest instanceof HttpServletRequest) {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
Principal userPrincipal = httpServletRequest.getUserPrincipal();
if (userPrincipal != null) {
actorId = userPrincipal.getName();
}
}
JbpmContext jbpmContext = getJbpmConfiguration(context).createJbpmContext(jbpmContextName);
servletRequest.setAttribute("jbpmContext",jbpmContext);
try {
if (isAuthenticationEnabled) {
jbpmContext.setActorId(actorId);
}
filterChain.doFilter(servletRequest, servletResponse);
} finally {
if(jbpmContext!=null){
try{
if(jbpmContext.getSession().isOpen()){
jbpmContext.close();
}
}catch(Exception e){
e.printStackTrace();
}
jbpmContext = null;
}
}
}
protected JbpmConfiguration getJbpmConfiguration(ServletContext servletContext) {
JbpmTemplate jbpmTemplate = (JbpmTemplate) WebApplicationContextUtils
.getWebApplicationContext(servletContext).getBean(
"jbpmTemplate");
JbpmConfiguration jconfig = jbpmTemplate.getJbpmConfiguration();
//return (JbpmConfiguration)SpringUtil.getInstance().getBean("jbpmConfiguration");
return jconfig;
}
public void destroy() {
}
}
(4)使用1
@Autowired
@Qualifier("jbpmConfiguration")
JbpmConfiguration jbpmConfiguration;
public JbpmContext getJbpmContext() {
return jbpmConfiguration.getCurrentJbpmContext();
}
JbpmContext jbpmContext = getJbpmContext();
使用2
JbpmContext jbpmContext=ServletActionContext.getRequest().getAttribute("jbpmContext");