隨筆-31  評論-2  文章-0  trackbacks-0

          web.xml的配置

          在web.xml中添加

          <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>
          classpath:/applicationContext-resources.xml
          classpath:/applicationContext-dao.xml
          classpath:/applicationContext-service.xml
          classpath*:/applicationContext.xml
          /WEB-INF/applicationContext*.xml
          /WEB-INF/security.xml
          /WEB-INF/dealer-security.xml
          </param-value>
          </context-param>

          <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
          <listener>
          <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
          </listener>
          <listener>
          <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
          </listener>

          事務的配置

          <aop:config>
          <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="execution(* *..service.*Manager.*(..))" order="0" />
          </aop:config>

          <tx:advice id="txAdvice" transaction-manager="transactionManager">
          <tx:attributes>
          <tx:method name="find*" read-only="true"/>
          <tx:method name="search*" read-only="true"/>
          <tx:method name="get*" read-only="true"/>
          <tx:method name="*" rollback-for="Throwable"/>
          </tx:attributes>
          </tx:advice>

          ApplicationContext.xml的配置(spring bean 的配置)

          <!--ProductManager-START-->
          <bean id="productManager" class="com.eryiju.service.product.impl.ProductManagerImpl">
          <constructor-arg ref="productDao" />
          <property name="brandDao" ref="brandDao"></property>
          </bean>

          <!--ProductManager-END-->
          <bean id="productDao" class="com.eryiju.dao.product.impl.ProductDaoHibernate">
          <property name="sessionFactory" ref="sessionFactory"></property>
          </bean>

          <bean id="brandDao" class="com.eryiju.dao.product.impl.BrandDaoHibernate">
          <property name="sessionFactory" ref="sessionFactory"></property>
          </bean>

          常見問題

          如何與hibernate整合

          <!-- Hibernate SessionFactory -->
          <bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
          <property name="dataSource" ref="dataSource" />
          <property name="configLocation" value="classpath:hibernate.cfg.xml" />
          <property name="hibernateProperties">
          <value>
          hibernate.dialect=${hibernate.dialect}
          hibernate.query.substitutions=true 'Y', false 'N'
          hibernate.cache.use_second_level_cache=true
          hibernate.cache.use_query_cache=true
          hibernate.cache.provider_class=com.eryiju.util.cache.EhCacheProvider
          hibernate.show_sql=false
          hibernate.connection.release_mode=after_statement
          hibernate.cglib.use_reflection_optimizer=false
          hibernate.search.default.directory_provider=org.hibernate.search.store.FSDirectoryProvider
          hibernate.search.default.indexBase=/opt/dev/static/index
          hibernate.jdbc.batch_size=50
          hibernate.jdbc.fetch_size=50
          </value>
          <!-- Turn batching off for better error messages under PostgreSQL -->
          </property>
          </bean>
          h2. (1)使用spring過濾器解決中文問題

          在web.xml中添加:
          <filter>
          <filter-name>Spring character encoding filter</filter-name>
          <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
          <init-param>
          <param-name>encoding</param-name>
          <param-value>GBK</param-value>
          </init-param>
          </filter>

          <filter-mapping>
          <filter-name>Spring character encoding filter</filter-name>
          <url-pattern>/*</url-pattern>
          </filter-mapping>

          h2. (2)將applicationContext.xml分解成多個文件

          applicationContext-common.xml
          "dataSource"
          "sessionFactory"
          事務相關

          applicationContext-dao.xml
          UserDAO

          applicationContext-biz.xml
          UserManager

          applicationContext-action.xml
          Action

          struts-config.xml中要作修改:
          <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
          <set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext-*.xml" />
          </plug-in>
          h2. Spring2.0中的注解實現事務管理

          第一步:引入<tx:>命名空間 ,在spring的配置文件中修改, beans根元素里多了三行,如下
          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:tx="http://www.springframework.org/schema/tx"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

          第二步:在spring的配置文件中修改,將所有具有@Transactional 注解的bean自動配置為聲明式事務支持
          Java代碼

          <!--JDBC事務管理器,根據你的情況使用不同的事務管理器,如果工程中有Hibernate,就用Hibernate的事務管理器 -->
          <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource">
          <ref local="dataSource"/>
          </property>
          </bean>

          <!-- 用注解來實現事務管理 -->
          <tx:annotation-driven transaction-manager="transactionManager"/>

          第三步: 在接口或類的聲明處 ,寫一個@Transactional. 要是只的接口上寫, 接口的實現類就會繼承下來.
          接口的實現類的具體方法,還可以覆蓋類聲明處的設置.
          Java代碼

          @Transactional
          public class TestPOAOImpl extends POAOBase implements TestPOAO
          {
          @Transactional(isolation = Isolation.READ_COMMITTED)
          public void test1()
          {
          String sql = "INSERT INTO sy_test (NAME,AGE) VALUES('注解趙云',30)";
          execute(sql);

          sql = "INSERT INTO sy_test (NAME,AGE) VALUES('注解張飛',26)";
          execute(sql);

          int a = 9 / 0; //異常

          sql = "INSERT INTO sy_test (NAME,AGE) VALUES('注解關羽',33)";
          execute(sql);
          System.out.println("走完了");
          }
          //execute() 方法略...
          }

          注意的幾點:

          1 @Transactional 只能被應用到public方法上, 對于其它非public的方法,如果標記了@Transactional也不會報錯,但方法沒有事務功能.
          2 默認情況下,一個有事務方法, 遇到RuntiomeException 時會回滾 . 遇到 受檢查的異常 是不會回滾 的. 要想所有異常都回滾,要加上 @Transactional( rollbackFor={Exception.class,其它異常}) .
          posted on 2009-07-02 09:40 xiaoxinchen 閱讀(187) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 大石桥市| 锦州市| 天全县| 河间市| 桃园市| 神农架林区| 普宁市| 沙河市| 安泽县| 扶绥县| 凭祥市| 崇文区| 张家港市| 临沧市| 观塘区| 北川| 改则县| 贵阳市| 夏邑县| 浮山县| 宽城| 洪雅县| 绵竹市| 泰兴市| 高陵县| 湖口县| 堆龙德庆县| 沁阳市| 科技| 灵宝市| 南川市| 阿拉善盟| 宁陵县| 永胜县| 工布江达县| 化德县| 红桥区| 深州市| 宜兰县| 大宁县| 巴彦县|