孤燈野火
          暢想的天空
          posts - 2,comments - 4,trackbacks - 0

          一.   定義頭:(為了后面定義標簽

          <?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"

                 xsi:schemaLocation="

                 http://www.springframework.org/schema/beans

                 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

                 http://www.springframework.org/schema/tx

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

                 http://www.springframework.org/schema/aop

                 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" default-lazy-init="true">

          二.定義數據源

                 <!-- 定義數據源的Bean ,給HibernatesessionFactory-->

              <bean id="dataSource"

                 class="org.apache.commons.dbcp.BasicDataSource">

                 <property name="driverClassName"

                     value="com.mysql.jdbc.Driver">

                 </property>

                 <property name="url"

                     value="jdbc:mysql://localhost:3306/test">

                 </property>

                 <property name="username" value="root"></property>

                 <property name="password" value="123456"></property>

              </bean>

          三.定義SessionFactory

          <bean id="sessionFactory"

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

                 <property name="lobHandler">

                     <ref bean="lobHandler"/>

                 </property>

                

                 <property name="dataSource">

                     <ref bean="dataSource" />

                 </property>

                 <property name="hibernateProperties">

                     <props>

                        <prop key="hibernate.dialect">

                            org.hibernate.dialect.MySQLDialect

                        </prop>

                        <!--設置二級緩沖-->

                        <prop key="hibernate.cache.provider_class">

                            org.hibernate.cache.EhCacheProvider

                        </prop>

                        <!--設置二級緩沖,打開查詢緩沖-->

                        <prop key="hibernate.cache.use_query_cache">true</prop>

                        <!--設置顯示Hibernate操作的SQL語句-->

                        <prop key="hibernate.show_sql">true</prop>

                     </props>

                 </property>

                 <property name="mappingResources">

                     <list>           

                        <value>

                            com/njwj/model/test.hbm.xml

                        </value>

                     </list>

                 </property>

              </bean>

          四.處理一些需要處理注入的Bean

          <!-- 申明處理Clob對象LobHandler -->

              <bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true" />

              <bean id="TestHandlerDao" class="com.njwj.dao.TestHandlerDao">

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

              </bean>

             

              <bean id="HandlerManager" class="com.njwj.service.HandlerManager">

              <property name="saveTestHanlerdao" ref="TestHandlerDao"></property>

              </bean>   

          配置事務管理器

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

              <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

                 <property name="sessionFactory">

                     <ref local="sessionFactory"/>

                 </property>

              </bean>

          六.配置事務管理特性

              <!-- 配置事務特性,配置adddeleteupdate開始的方法,事務傳播特性為required-->      

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

              <tx:attributes>

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

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

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

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

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

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

              </tx:attributes>

          </tx:advice>


          七.配置哪些類進行事務管理

              <!-- 配置那些類的方法進行事務管理,當前com.njwj.service包中的子包、類中所有方法需要,還需要參考tx:advice的設置 -->

              <aop:config>

              <aop:pointcut id="allManagerMethod" expression="execution (* com.njwj.service.*.*(..))"/>

              <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>

          </aop:config>

           

          八.配置Spring采用annotation(可選)

              <context:annotation-config />


          Web配置

          <!--定義Spring的配置的位置,可以定義多個配置文件,可以使用通配符。 -->

              <context-param>

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

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

              </context-param>

              <!--設置一起動當前的Web應用,就加載Spring,讓Spring管理Bean-->

              <listener>

                 <listener-class>

                     org.springframework.web.context.ContextLoaderListener

                 </listener-class>

              </listener>

                 <!--解決Hibernate延遲加載出現的問題,需要放到struts2過濾器之前-->

              <filter>

                 <filter-name>lazyLoadingFilter</filter-name>

                 <filter-class>

                     org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

                 </filter-class>

              </filter>

             

                 <!--Struts2的過濾器,使用Struts2,必須配置該項-->

              <filter>

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

                 <filter-class>

                     org.apache.struts2.dispatcher.FilterDispatcher

                 </filter-class>

              </filter> 

             

              <!--解決Hibernate延遲加載出現的問題,仍需要放到struts2過濾器mapping之前-->

          <filter-mapping>

                 <filter-name>lazyLoadingFilter</filter-name>

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

              </filter-mapping>

          <!--Struts2的過濾器,配套的filter-mapping-->

              <filter-mapping>

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

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

              </filter-mapping>

           

           

          posted on 2009-05-10 21:33 孤飛燕 閱讀(1934) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 嫩江县| 新建县| 抚松县| 六枝特区| 灯塔市| 准格尔旗| 深泽县| 绍兴县| 吉隆县| 星座| 垦利县| 永仁县| 融水| 大竹县| 通辽市| 玉龙| 维西| 安岳县| 延津县| 菏泽市| 龙口市| 鄂温| 惠州市| 东阿县| 肥乡县| 延长县| 巢湖市| 贡嘎县| 安福县| 本溪| 永嘉县| 宜州市| 定州市| 重庆市| 迁安市| 东宁县| 华安县| 东莞市| 西林县| 德钦县| 府谷县|