struts2.3.20+spring4.0.2+hibernate4.3.4框架整合
项目需求,整合新版本的SSH框架,记录一下以备后查
下载链接:
各版本需要的包如下:
struts2.3.20
spring4.0.2
hibernate4.3.4
其它依赖包
其中hibernate依赖包中包含了C3PO数据池的包c3p0-0.9.2.1.jar、hibernate-c3p0-4.2.16.Final.jar和mchange-commons-java-0.2.3.4.jar
在其他依赖包中包含了DBCP数据池的包commons-dbcp-1.2.2.jar和commons-pool.jar
可根据需要自行删减。其它依赖包中jxl.jar为操作Excel的包,mysql-connector-java-5.0.8-bin.jar为数据库驱动包
配置说明:
struts.xml配置
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 3 "http://struts.apache.org/dtds/struts-2.3.dtd"> 4 <struts> 5 <constant name="struts.objectFactory" value="spring"></constant> 6 <constant name="struts.devMode" value="true"></constant> 7 <constant name="struts.multipart.saveDir" value="/upload"/> 8 <constant name="struts.multipart.maxSize" value="824288000000"/> 9 <constant name="struts.action.extension" value=","/> 10 11 <package name="app-store-struts" extends="struts-default"> 12 <action name="sc_*" class="ServerConnection" method="{1}"></action> 13 </package> 14 <package name="app-manage-struts" extends="struts-default" namespace="/manage"> 15 <global-results> 16 <result name="platformLoginPage" >/manage/xxxLogin.jsp</result> 17 <result name="manageIndexPage" >/manage/index.jsp</result> 18 </global-results> 19 <action name="aa_*" class="xxxyAction" method="{1}"></action> 20 <action name="admin_*" class="xxxAction" method="{1}"> 21 <result name="redirectIndexPage" type="redirectAction"> 22 <param name="actionName">admin_index</param> 23 <param name="namespace">/manage</param> 24 </result> 25 <result name="toUserVisitPage">/manage/xxx.html</result> 26 </action> 27 </package> 28 </struts>
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 3 <import resource="applicationContextAction.xml"/> 4 <import resource="applicationContextService.xml"/> 5 <import resource="applicationContextDao.xml"/> 6 <import resource="applicationContextHibernate.xml"/> 7 </beans>
applicationContextAction.xml、applicationContextService.xml和applicationContextDao.xml配置格式一致,如下
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "../dtd/spring-beans-2.0.dtd" > 3 4 <beans default-autowire="autodetect"> 5 <bean id="ServerConnection" class="com.xxx.ServerConnection" scope="prototype"> 6 <property name="xxService" ref="xxService"></property> 7 </bean> 8 ...... 9 </beans>
applicationContextHibernate.xml配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-4.0.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> 15 <!-- dbcp数据源,连接数据库 --> 16 <bean id="dataSourcexxx" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 17 <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 18 <property name="url" value="jdbc:mysql://127.0.0.1:3306/xx" /> 19 <property name="username" value="xxx" /> 20 <property name="password" value="xxx" /> 21 22 <property name="initialSize" value="10"/><!-- 初始化连接 --> 23 <property name="maxIdle" value="50"/><!-- 最大空闲连接 --> 24 <property name="minIdle" value="10"/><!-- 最小空闲连接 --> 25 <property name="maxActive" value="300"/><!-- 最大连接数量 --> 26 <property name="logAbandoned" value="true"/><!-- 是否在自动回收超时连接的时候打印连接的超时错误 --> 27 <property name="removeAbandoned" value="true"/><!-- 是否自动回收超时连接 --> 28 <property name="removeAbandonedTimeout" value="180"/><!-- 超时时间(以秒数为单位) --> 29 <property name="maxWait" value="1000"/> <!--超时等待时间以毫秒为单位 --> 30 31 <property name="testOnBorrow" value="false"/> <!-- 数据库连接池中取得连接时,对其的有效性进行检查 ,会影响一定性能--> 32 <property name="testWhileIdle" value="true"/> <!--异步Evict的TimerTask定时线程进行控制 定时对线程池中的链接进行validateObject校验--> 33 <property name="timeBetweenEvictionRunsMillis" value="25200000"/><!--失效检查线程运行时间间隔(毫米) 大于0才会开启evict检查线程--> 34 <property name="validationQuery" value="select 1"/> <!-- 校验sql--> 35 </bean> 36 37 38 <!-- 连接数据源 --> 39 <bean id="sessionFactoryxxx" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 40 <property name="dataSource"> 41 <ref bean="dataSourcexxx"/> 42 </property> 43 <property name="mappingResources"> 44 <list> 45 <value>com/xxx/xxx.hbm.xml</value> 46 <value>com/xxx/xyyyy.hbm.xml</value> 47 </list> 48 </property> 49 <property name="hibernateProperties"> 50 <props> 51 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 52 <prop key="hibernate.show_sql">false</prop> 53 <prop key="hibernate.cache.use_second_level_cache">true</prop> 54 </props> 55 </property> 56 </bean> 57 <!-- spring aop的使用,控制事务 --> 58 <!-- 配置事务管理器 --> 59 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 60 <property name="sessionFactory"> 61 <ref bean="sessionFactoryxxx" /> 62 </property> 63 </bean> 64 <!-- 65 <tx:annotation-driven transaction-manager="transactionManager"/> 66 --> 67 <tx:advice id="TestAdvice" transaction-manager="transactionManager"> 68 <!-- 配置事务传播特性(根据事务管理器配置通知) --> 69 <tx:attributes> 70 <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> 71 <tx:method name="del*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> 72 <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> 73 <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> 74 <tx:method name="change*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> 75 <tx:method name="set*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> 76 <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> 77 <tx:method name="new*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> 78 <tx:method name="find*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> 79 <tx:method name="get*" propagation="REQUIRED" read-only="true" /> 80 <tx:method name="select*" propagation="REQUIRED" read-only="true" /> 81 <tx:method name="list*" propagation="REQUIRED" read-only="true" /> 82 <tx:method name="*" propagation="REQUIRED"/> 83 </tx:attributes> 84 </tx:advice> 85 86 <!-- 配置参与所有service事务的类 --> 87 <aop:config> 88 <aop:pointcut id="allTestServiceMethod" expression="execution(* com.xxx.service.impl.*.*(..))|| 89 execution(* com.xxx.yyy.impl.*.*(..))" /> 90 <aop:advisor advice-ref="TestAdvice" pointcut-ref="allTestServiceMethod" /> 91 </aop:config> 92 <!-- 配置日志记录的类 --> 93 <bean id="logAspectService" class="com.xxx.log.LogAspect"> 94 <property name="logDao" ref="logDao"></property> 95 </bean> 96 <aop:config> 97 <aop:pointcut id="logPointCut" 98 expression="execution(* com.xxx.service.impl.*.add*(..))||execution(* com.xxx.service.impl.*.del*(..))||execution(* com.xxx.service.impl.*.upd*(..))||execution(* com.xxx.service.impl.*.edit*(..)) 99 ||execution(* com.xxx.service.impl.*.remove*(..))||execution(* com.xxx.service.impl.*.master*(..))" /> 100 <aop:aspect id="logAspect" ref="logAspectService"> 101 <aop:after method="log" pointcut-ref="logPointCut"/> 102 </aop:aspect> 103 </aop:config> 104 </beans>