天天看点

OA中SSH+JBPM项目整合

一,集成 spring 与 hibernate

 1,配置sessionfactory

  1,配置

   ---------------------- applicationcontext.xml ------------------------

   <!-- 配置sessionfactory(整合hibernate) -->

   <context:property-placeholder location="classpath:jdbc.properties" />

   <bean id="sessionfactory"

    class="org.springframework.orm.hibernate3.localsessionfactorybean">

    <property name="datasource">

     <bean class="com.mchange.v2.c3p0.combopooleddatasource">

      <!-- 数据库连接信息 -->

      <property name="jdbcurl" value="${jdbcurl}"></property>

      <property name="driverclass" value="${driverclass}"></property>

      <property name="user" value="${username}"></property>

      <property name="password" value="${password}"></property>

      <!-- 其他配置 -->

      <property name="initialpoolsize" value="3"></property>

      <property name="maxpoolsize" value="5"></property>

      <property name="minpoolsize" value="3"></property>

      <property name="acquireincrement" value="2"></property>

      <property name="maxstatements" value="8"></property>

      <property name="maxstatementsperconnection" value="5"></property>

      <property name="maxidletime" value="20"></property>

     </bean>

    </property>

    <!-- 指定hibernate的配置文件的位置 -->

    <property name="configlocation" value="classpath:hibernate.cfg.xml"></property>

   </bean>

   ---------------------- jdbc.properties ------------------------

   jdbcurl = jdbc:mysql:///itcastoa

   driverclass = com.mysql.jdbc.driver

   username = root

   password = 1234

  2,测试代码

   @test// 测试 sessionfactory 的配置

   public void testsessionfactory(){

    sessionfactory sessionfactory = (sessionfactory) ac.getbean("sessionfactory");

    assert.assertnotnull(sessionfactory.opensession());

   }

 2,配置声明式事务(使用基于注解的方式)

   <!-- 配置事务管理器 -->

   <bean id="transactionmanager" class="org.springframework.orm.hibernate3.hibernatetransactionmanager">

    <property name="sessionfactory" ref="sessionfactory"></property>

   <!-- 配置基于注解的事务支持-->

   <tx:annotation-driven transaction-manager="transactionmanager"/>

   1,service类

    @service

    public class insertuserservice {

     @resource

     private sessionfactory sessionfactory;

     @transactional

     public void addusers() {

      sessionfactory.getcurrentsession().save(new user());

      // int a = 1 / 0; // 这行会抛异常

     }

    }

   2,单元测试

    @test // 测试声明式事务

    public void testtransaction() {

     insertuserservice service = (insertuserservice) ac.getbean("insertuserservice");

     service.addusers();

 3,在web.xml中配置 spring 的 opensessioninview 过滤器(解决抛lazyinitializationexception的问题)

   <!-- 配置 spring 的 opensessioninview 过滤器 -->

   <filter>

    <filter-name>opensessioninview</filter-name>

    <filter-class>org.springframework.orm.hibernate3.support.opensessioninviewfilter</filter-class>

   </filter>

   <filter-mapping>

    <url-pattern>*.do</url-pattern>

   </filter-mapping>

  2,lazyinitializationexception异常说明

   1,对于集合属性,默认是lazy="true"的,在第一次使用时才加载。

   2,但在加载时,如果session已经关掉了就会抛lazyinitializationexception异常

二,集成 spring 与 struts2.1.8.1

 1,在web.xml配置监听器(spring reference 15.2 common configuration)

  <!-- 集成spring -->

  <listener>

   <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>

  </listener>

  <context-param>

   <param-name>contextconfiglocation</param-name>

   <param-value>/web-inf/classes/applicationcontext*.xml</param-value>

  </context-param>

 2,在struts-config.xml中配置controller(spring reference 15.4.1.1. delegatingrequestprocessor)

  <controller>

   <set-property property="processorclass" value="org.springframework.web.struts.delegatingrequestprocessor" />

  </controller>

 3,测试

  1,写action类与service类

   @controller("testaction")

   @scope("prototype")

   public class testaction extends actionsupport {

    @resource

    private testservice testservice;

    @override

    public string execute(){

     testservice.savetwouser();

     return success;

   @service

   public class testservice {

    private sessionfactory sessionfactory;

    @transactional

    public void savetwouser() {

     sessionfactory.getcurrentsession().save(new user());

     // int a = 1 / 0; // 这行会抛异常

  2,在struts.xml中配置action

   <!-- 测试 -->

   <action name="test" class="testaction">

    <result>/test.jsp</result>

   </action>

  3,部署到tomcat中并访问测试。

 4,说明:

  1,在写action时要指定 @controller 与 @scope("prototype")

  2,在struts.xml中配置action时,在class属性中写bean的名称

三,整合spring与jbpm4(jbpm4.4 developers guide, chapter 17. spring integration) 

 1,在jbpm.cfg.xml中

  1,删除配置:

   <import resource="jbpm.tx.hibernate.cfg.xml" />

  2,增加配置:

   <!-- 整合spring -->

   <import resource="jbpm.tx.spring.cfg.xml" />

 2,在applicationcontext.xml中配置

  <!-- 配置processengine(整合jbpm4) -->

  <bean id="springhelper" class="org.jbpm.pvm.internal.processengine.springhelper">

   <!-- jbpmcfg是相对于classpath的相对路径,默认值为jbpm.cfg.xml -->

   <property name="jbpmcfg" value="jbpm.cfg.xml"></property>

  </bean>

  <bean id="processengine" factory-bean="springhelper" factory-method="createprocessengine" />

  @test // 测试processengine

  public void testprocessengine() {

   processengine processengine = (processengine) ac.getbean("processengine");

   assert.assertnotnull(processengine);

  }