zoukankan      html  css  js  c++  java
  • SSH深度历险(六) 深入浅出----- Spring事务配置的五种方式

            这对时间在学习SSH中Spring架构,Spring的事务配置做了具体总结。在此之间对Spring的事务配置仅仅是停留在听说的阶段,总结一下。总体把控。通过这次的学习发觉Spring的事务配置仅仅要把思路理清,还是比較好掌握的。

        总结例如以下:

        Spring配置文件里关于事务配置总是由三个组成部分,各自是DataSource、TransactionManager和代理机制这三部分。不管哪种配置方式。一般变化的仅仅是代理机制这部分。

        DataSource、TransactionManager这两部分仅仅是会依据数据訪问方式有所变化,比方使用Hibernate进行数据訪问时。DataSource实际为SessionFactory。TransactionManager的实现为HibernateTransactionManager。

        详细例如以下图:

     

        依据代理机制的不同,总结了五种Spring事务的配置方式,配置文件例如以下:

        第一种方式:每一个Bean都有一个代理

    <span style="font-size:18px;"><span style="font-size:18px;"><?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: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"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> </bean> <!-- 定义事务管理器(声明式的事务) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置DAO --> <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="userDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <!-- 配置事务管理器 --> <property name="transactionManager" ref="transactionManager" /> <property name="target" ref="userDaoTarget" /> <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" /> <!-- 配置事务属性 --> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> </beans></span></span>




        另外一种方式:全部Bean共享一个代理基类

    <span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><?

    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: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"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> </bean> <!-- 定义事务管理器(声明式的事务) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="transactionBase" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true"> <!-- 配置事务管理器 --> <property name="transactionManager" ref="transactionManager" /> <!-- 配置事务属性 --> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 配置DAO --> <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="userDao" parent="transactionBase" > <property name="target" ref="userDaoTarget" /> </bean> </beans></span></span></span>

    第三种方式:使用拦截器


    <span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><?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: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">
    
        <bean id="sessionFactory"  
                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
            <property name="configLocation" value="classpath:hibernate.cfg.xml" />  
            <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        </bean>  
    
        <!-- 定义事务管理器(声明式的事务) -->  
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean> 
       
        <bean id="transactionInterceptor"  
            class="org.springframework.transaction.interceptor.TransactionInterceptor">  
            <property name="transactionManager" ref="transactionManager" />  
            <!-- 配置事务属性 -->  
            <property name="transactionAttributes">  
                <props>  
                    <prop key="*">PROPAGATION_REQUIRED</prop>  
                </props>  
            </property>  
        </bean>
          
        <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
            <property name="beanNames">  
                <list>  
                    <value>*Dao</value>
                </list>  
            </property>  
            <property name="interceptorNames">  
                <list>  
                    <value>transactionInterceptor</value>  
                </list>  
            </property>  
        </bean>  
      
        <!-- 配置DAO -->
        <bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    </beans></span></span></span>
    第四种方式:使用tx标签配置的拦截器
    <span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><?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:context="http://www.springframework.org/schema/context"
        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/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
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
        <context:annotation-config />
        <context:component-scan base-package="com.bluesky" />
    
        <bean id="sessionFactory"  
                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
            <property name="configLocation" value="classpath:hibernate.cfg.xml" />  
            <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        </bean>  
    
        <!-- 定义事务管理器(声明式的事务) -->  
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="*" propagation="REQUIRED" />
            </tx:attributes>
        </tx:advice>
        
        <aop:config>
            <aop:pointcut id="interceptorPointCuts"
                expression="execution(* com.bluesky.spring.dao.*.*(..))" />
            <aop:advisor advice-ref="txAdvice"
                pointcut-ref="interceptorPointCuts" />        
        </aop:config>      
    </beans></span></span></span>

    第五种方式:全注解
    <span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"><?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:context="http://www.springframework.org/schema/context" 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/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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.bluesky" /> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> </bean> <!-- 定义事务管理器(声明式的事务) --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans></span></span></span>




    此时在DAO上需加上@Transactional注解,例如以下:
    <span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;">package com.bluesky.spring.dao;
    
    import java.util.List;
    
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    import org.springframework.stereotype.Component;
    
    import com.bluesky.spring.domain.User;
    
    @Transactional
    @Component("userDao")
    public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
    
        public List<User> listUsers() {
            return this.getSession().createQuery("from User").list();
        }
        
        
    }</span></span></span>


    总结



    XML配置方式



    长处:
            1. XML配置方式进一步减少了耦合,使得应用更加easy扩展。即使对配置文件进一步改动也不须要project进行改动和又一次编译。


            2.在处理大的业务量的时候,用XML配置应该更加好一些。

    由于XML更加清晰的表明了各个对象之间的关系,各个业务类之间的调用。同一时候spring的相关配置也能一目了然。
    当然,有人会说,用XML配置,在大的业务量时候会使得XML文件过大,不easy查看。

    这一点我们全然能够利用业务分解书写多个XML配置文件就能够了。

    缺点:
            配置文件读取和解析须要花费一定的时间,配置文件过多的时候难以管理。无法对配置的正确性进行校验,添加了測试难度。



    annotation配置的优缺点:

    长处:


    1. 在class文件里。能够减少维护成本,annotation的配置机制非常明显简单
    2. 不须要第三方的解析工具,利用java反射技术就能够完毕任务
    3. 编辑期能够验证正确性,差错变得easy
    4. 提高开发效率

    缺点:


    1. 假设须要对于annotation进行改动,那么要又一次编译整个project
    2. 业务类之间的关系不如XML配置那样easy把握。


    3. 假设在程序中annotation比較多,直接影响代码质量。对于代码的简洁度有一定的影响。



             SSH内容非常多。先从宏观把控。在项目中历练……多多总结、思路清晰;为了提高系统的灵活性与减少维护成本,提高效率使用到了非常多的配置文件来回调用、返回。涉及到非常多联系,这个时候真的有必要绘图。一切尽在图中展示吧,画出自己的思维方式。

  • 相关阅读:
    table中tr间距的设定table合并单元格 colspan(跨列)和rowspan(跨行)
    使用jquery触发a标签跳转
    真正的让iframe自适应高度 兼容多种浏览器随着窗口大小改变
    html5 data属性的使用
    jQuery取得select选择的文本与值
    jqueryui教程
    密码复杂度
    zabbix配置微信报警
    tomcat配置域名访问
    阿里云ecs禁止ping,禁止telnet
  • 原文地址:https://www.cnblogs.com/llguanli/p/6949772.html
Copyright © 2011-2022 走看看