<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-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 閱讀(185) 評論(0)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 最刺激黄a大片免费网站| 久久这里只精品99re免费| 在线观看视频免费完整版| 日韩国产免费一区二区三区| 伊人免费在线观看| 污视频在线观看免费| 久久精品免费观看国产| 亚洲国产美女精品久久久久∴| 暖暖免费高清日本一区二区三区| **真实毛片免费观看| 国产成人免费手机在线观看视频| 拍拍拍又黄又爽无挡视频免费| 热re99久久6国产精品免费| 久久午夜无码免费| 亚洲国产综合专区电影在线 | 国产精品偷伦视频免费观看了| 国产精品亚洲专区一区| 久久99免费视频| 亚洲精品A在线观看| 亚洲成av人片不卡无码| 一级午夜免费视频| 成年轻人网站色免费看| 亚洲另类激情综合偷自拍| 亚欧在线精品免费观看一区| 亚洲爆乳无码专区| 美女视频黄.免费网址| 91在线免费观看| 911精品国产亚洲日本美国韩国| 亚洲区视频在线观看| 亚洲熟妇无码av另类vr影视| 久久午夜免费鲁丝片| 久久久久se色偷偷亚洲精品av| 亚洲爆乳无码精品AAA片蜜桃| 亚洲熟伦熟女专区hd高清| 免费人成在线观看播放国产| 亚洲色大成网站www永久一区 | 亚洲日本香蕉视频观看视频| 99在线视频免费观看| 国产亚洲人成A在线V网站| 亚洲日本一区二区| 久久免费精彩视频|