Windows
STS
Mysql5
Jdk1.8
Tomcat8
Maven3
Struts2 + Spring4 + Hibernate3 + Activiti5.17
application-config.xml内容:
<!-- 加载数据库配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源c3p0的方式 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!-- =========== 数据库连接信息 =========== -->
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
<!-- =========== 连接池的管理配置 =========== -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/activiti?autoReconnect=true
user=root
password=root
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入dataSource -->
<property name="dataSource" ref="dataSource"/>
<!-- 引入hibernate.cfg.xml, 指定hibernate的配置文件的位置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
// 测试SessionFactory
@Test
public void testSessionFactory() throws Exception {
SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
Assert.assertNotNull(sessionFactory.openSession());
ctx.destroy();
}
<!-- 开启基于注解的事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 设置hibernate3的事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
Spring 的 OpenSessionInView 过滤器(解决抛LazyInitializationException异常的问题)
<!-- 配置 spring 的 OpenSessionInView 过滤器 -->
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
LazyInitializationException异常说明
1,对于集合属性,默认是lazy=”true”的,在第一次使用时才加载。
2,但在加载时,如果Session已经关掉了就会抛LazyInitializationException异常
<!-- 配置Spring的用于初始化ApplicationContext对象的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置Struts2的核心的过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<struts>
<constant name="struts.action.extension" value="do"></constant>
<package name="dengfengdecao" namespace="/" extends="struts-default">
<action name="user_*" class="userAction" method="{1}">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
当引入这个插件后,原先所struts创建的action类,交给了spring创建。在struts2-spring-plugin.jar中有一个struts-plugin.xml,里面声明了action类由spring工厂创建。
这个插件重写了struts的对象工厂,当创建一个action类时,它会根据struts的配置文件的class属性的值与spring配置文件中的id属性的值相匹配。如果没有与之想匹配,将会像没有使用这个插件前一样创建,然后由spring自动装配。
说明:
1,在写Action时要指定 @Controller 与 @Scope(“prototype”);
2,在struts.xml中配置action时,在class属性中写bean的名称。
在pom.xml中添加一下内容
<!-- 设置虚拟路径,并自动启动tomcat -->
<build>
<!-- 如果发布到根目录则改为ROOT -->
<finalName>activiti-ssh</finalName>
<plugins>
<plugin>
<!-- org.codehaus.cargo插件依赖 -->
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.15</version>
<configuration>
<container>
<containerId>tomcat8x</containerId>
<home>D:/Dev/Tomcat8</home>
</container>
<configuration>
<type>existing</type>
<home>D:/Dev/Tomcat8</home>
</configuration>
</configuration>
<executions>
<execution>
<id>cargo-run</id>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
在~/.m2/setting.xml 中添加org.codehaus.cargo插件支持
<pluginGroup>org.codehaus.cargo</pluginGroup>
右键pom.xml 》maven install 启动tomcat后,在地址栏输入
http://localhost:8080/activiti-ssh 即可访问首页。
<!-- 配置activiti引擎 -->
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="true" />
<property name="jobExecutorActivate" value="false" />
</bean>
<!-- 加载activiti引擎 -->
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
<!-- activiti的各种服务接口 -->
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
<bean id="formService" factory-bean="processEngine" factory-method="getFormService" />
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />
<!-- 引入activiti的配置文件 -->
<import resource="activiti-context.xml"/>
// 测试ProcessEngine
@Test
public void testProcessEngine() {
ProcessEngine processEngine = (ProcessEngine) ac.getBean("processEngine");
Assert.assertNotNull(processEngine);
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.dengfengdecao.demo</groupId>
<artifactId>activiti-ssh</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<!-- Generic properties -->
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Web -->
<servlet.version>3.1.0</servlet.version>
<jstl.version>1.2</jstl.version>
<!-- Activiti & activiti-spring 依赖4.0.6.RELEASE版本的spring-->
<activiti.version>5.17.0</activiti.version>
<activiti-spring.version>5.17.0</activiti-spring.version>
<!-- Struts2 -->
<struts2.version>2.3.20</struts2.version>
<!-- struts2-spring-plugin -->
<struts2-spring.version>2.3.20</struts2-spring.version>
<!-- Spring -->
<spring-framework.version>4.0.6.RELEASE</spring-framework.version>
<!-- Hibernate / JPA -->
<hibernate.version>3.6.10.Final</hibernate.version>
<!-- DataSource:c3p0 & dbcp2 -->
<c3p0.version>0.9.5.1</c3p0.version>
<dbcp2.version>2.0</dbcp2.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- Other Web dependencies -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
<scope>provided</scope>
</dependency>
<!-- activiti -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>${activiti-spring.version}</version>
</dependency>
<!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
</dependency>
<!-- struts2-spring-plugin 与整合spring -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts2-spring.version}</version>
</dependency>
<!-- Spring & Transactions & ORM-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- mysql5 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<!-- DataSource 择一其可-->
<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<!-- dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>${dbcp2.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 设置虚拟路径,并自动启动tomcat -->
<build>
<!-- 如果发布到根目录则改为ROOT -->
<finalName>activiti-ssh</finalName>
<plugins>
<plugin>
<!-- org.codehaus.cargo插件依赖 -->
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.15</version>
<configuration>
<container>
<containerId>tomcat8x</containerId>
<home>D:/Dev/Tomcat8</home>
</container>
<configuration>
<type>existing</type>
<home>D:/Dev/Tomcat8</home>
</configuration>
</configuration>
<executions>
<execution>
<id>cargo-run</id>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
最后挂上Demo地址:http://download.csdn.net/detail/u011726984/8956879
欢迎转载