Struts2+Spring2.5+Hibernate3.3 整合總結

1.       導入: (struts2.0+spring)包+

 hibernate3.jar

         antlr-2.7.6.jar

                    commons-collections-3.1.jar

         dom4j-1.6.1.jar

         javassist-3.4.GA.jar

jta-1.1.jar

slf4j-api-1.5.2.jar

slf4j-log4j12-1.5.2.jar

log4j-1.2.15.jar

aspectjrt.jar

aspectjweaver.jar

 

 

2.1將applicationContext.xml剪切到src目錄

2.2將applicationContext.xml從命名applicationContext-actions.xml

2.3添加applicationContext-common.xml

2.4添加applicationContext-beans.xml

applicationContext-common.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xmlns:aop="http://www.springframework.org/schema/aop"

         xmlns:tx="http://www.springframework.org/schema/tx"

         xmlns:lang="http://www.springframework.org/schema/lang"

         xsi:schemaLocation="http://www.springframework.org/schema/beans                                                             http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

          http://www.springframework.org/schema/lang                                                                          http://www.springframework.org/schema/lang/spring-lang-2.0.xsd                                    http://www.springframework.org/schema/tx                                                                        http://www.springframework.org/schema/tx/spring-tx.xsd

          http://www.springframework.org/schema/aop                                                                   http://www.springframework.org/schema/aop/spring-aop.xsd">

 

         <!-- 配置sessionFactory -->

         <bean id="sessionFactory"

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

                   <property name="configLocation">

                            <value>classpath:hibernate.cfg.xml</value>

                   </property>

         </bean>

 

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

         <bean id="transactionManager"

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

                   <property name="sessionFactory">

                            <ref bean="sessionFactory" />

                   </property>

         </bean>

 

         <!-- 配置事物傳播特性 -->

         <tx:advice id="txAdvice" transaction-manager="transactionManager">

                   <tx:attributes>

                            <tx:method name="add*" propagation="REQUIRED" />

                            <tx:method name="delete*" propagation="REQUIRED" />

                            <tx:method name="modify*" propagation="REQUIRED" />

                            <tx:method name="*" read-only="true" />

                   </tx:attributes>

         </tx:advice>

 

         <!-- 哪些類的哪些方法參與事物, (* com.evan.crm.service.*.*(..))中幾個通配符的含義:

                   第一個 * —— 通配 任意返回值類型

                   第二個 * —— 通配 包com.evan.crm.service下的任意class

                   第三個 * —— 通配 包com.evan.crm.service下的任意class的任意方法

                   第四個 .. —— 通配 方法可以有0個或多個參數

                   綜上:包com.evan.crm.service下的任意class的具有任意返回值類型、任意數目參數和任意名稱的方法-->

         <aop:config>

                   <aop:pointcut id="allManagerMethod"

                            expression="execution(* com.struts2.service.*.*(..))" />

                   <aop:advisor pointcut-ref="allManagerMethod"

                            advice-ref="txAdvice" />

         </aop:config>

</beans>

 

 

3修改web.xml配置文件:

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

         <context-param>

                   <param-name>contextConfigLocation</param-name>

                   <param-value>classpath*:applicationContext-*.xml</param-value>

         </context-param>

 

         <!-- 啓動Spring,默認讀取WEB-INF目錄下的applicationContext.xml -->

         <listener>

                   <listener-class>

                            org.springframework.web.context.ContextLoaderListener

                   </listener-class>

         </listener>

 

         <!--這個過濾器配置,爲了將Hibernate的session對象的的關閉和打開交給spring管理, -->

         <filter>

                   <filter-name>hibernateFilter</filter-name>

          <filter-class>

org.springframework.orm.hivernate3.support.OpenSessionInViewFilter

</filter-class>

         </filter>

         <filter-mapping>

                   <filter-name>hibernateFilter</filter-name>

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

         </filter-mapping>

 

         <!-- 配置Struts2過濾器 -->

         <filter>

                   <filter-name>struts2</filter-name>

                   <filter-class>

                            org.apache.struts2.dispatcher.FilterDispatcher

                   </filter-class>

         </filter>

         <filter-mapping>

                   <filter-name>struts2</filter-name>

                   <url-pattern>/*</url-pattern>

         </filter-mapping>

</web-app>

 

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/salasay/archive/2009/03/03/3954650.aspx

發佈了54 篇原創文章 · 獲贊 1 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章