首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring的事物配备方式

2012-11-11 
spring的事物配置方式?根据代理机制不同,有以下几种配置方式:(推荐使用第四种方式,做spring的事物)?首先定

spring的事物配置方式

?

根据代理机制不同,有以下几种配置方式:(推荐使用第四种方式,做spring的事物)

?

首先定义一个接口和实现类,并在此基础上进行配置---

Java代码??

spring的事物配备方式

public interface IUserDao {   public void insertUser(UserTable user);  }   
?

Java代码??

spring的事物配备方式

public class UserDaoImpl extends HibernateDaoSupport implements IUserDao{      public void insertUser(UserTable user) {          getHibernateTemplate().saveOrUpdate(user);      }  }   
?

?

  • 第一种:每个bean设置一个代理,这种是根据具体需求来定,如要对具体到每个交易进行事务操作的话,这个方式是最合适的

    ?

    <!--?每个bean都有一个代理?-->??

    <beans>      <!-- sessionFactory相当于spring datasource -->      <bean id="sessionFactory" value="classpath:hibernate.cfg.xml" />            <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />      </bean>        <!-- 定义事务管理器(声明式的事务) -->        <bean id="transactionManager"          ref="sessionFactory" />      </bean>            <!-- 配置DAO -->      <bean id="userDao" ref="sessionFactory" />      </bean>      <!-- 每个bean都有一个代理 <property name="target" ref="userDaoTarget" />  -->      <bean id="userDaoProxy"            ref="transactionManager" />               <property name="target" ref="userDao" />             <property name="proxyInterfaces" value="org.lgh.spring.transaction2.IUserDao" />          <!-- 配置事务属性 -->            <property name="transactionAttributes">                <props>                    <prop key="*">PROPAGATION_REQUIRED</prop>              </props>            </property>        </bean>    </beans>  
    ?

    ?

    • 第二种:所有的bean共享一个代理

      ?

       <!-- 所有的bean共享一个代理-->  <beans>      <bean id="sessionFactory"                value="classpath:hibernate.cfg.xml" />            <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />      </bean>        <!-- 定义事务管理器(声明式的事务) -->        <bean id="transactionManager"          ref="sessionFactory" />      </bean>      <!-- 所有的bean共享一个代理/>  -->      <bean id="transactionBase"                               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="userDao" ref="sessionFactory" />      </bean>       <!-- 所有的bean共享一个代理/>  -->      <bean id="userDaoProxy" parent="transactionBase" >            <property name="target" ref="userDao" />         </bean>  </beans>  
      ?

      ?

      • 第三种:使用拦截器 来配置你的事务,这个主要是进行一些方法调用前后进行一些其他事件的处理,如进行权限检查等...

        ?

        <!-- 使用拦截器 -->    <beans>      <bean id="sessionFactory"                value="classpath:hibernate.cfg.xml" />            <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />      </bean>        <!-- 定义事务管理器(声明式的事务) -->        <bean id="transactionManager"          ref="sessionFactory" />      </bean>       <bean id="transactionInterceptor"            ref="transactionManager" />            <!-- 配置事务属性 -->            <property name="transactionAttributes">                <props>                    <prop key="*">PROPAGATION_REQUIRED</prop>                </props>            </property>        </bean>      <bean name="logger" ref="sessionFactory" />      </bean>      <!-- 服务层 service-->       <bean id="userService" ref="userDao" />      </bean>  </beans>  
        ?

        ?

        • 第四种:使用aop:config配置方式

          ?

          <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="org.lgh.spring.transaction5" />      <bean id="sessionFactory"                value="classpath:hibernate.cfg.xml" />            <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />      </bean>        <!-- 定义事务管理器(声明式的事务) -->        <bean id="transactionManager"          ref="sessionFactory" />      </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="update*" propagation="REQUIRED" /><tx:method name="add*" propagation="REQUIRED" /><tx:method name="*" propagation="REQUIRED" />  </tx:attributes>      </tx:advice>      <aop:config   proxy-target->          <aop:pointcut id="interceptorPointCuts"              expression="execution(* org.lgh.spring.transaction5.*.*(..))" />          <aop:advisor advice-ref="txAdvice"              pointcut-ref="interceptorPointCuts" />              </aop:config>           <!-- 配置DAO -->      <bean id="userDao" ref="sessionFactory" />      </bean>  </beans>  
          ?

          ?

          ?

          execution(*?org.lgh.spring.transaction5.*.*(..))中:

          第一个* —— 通配 任意返回值类型|

          |第二个 * —— 通配 包org.lgh.spring.transaction5下的任意class|

          |第三个 * —— 通配 包org.lgh.spring.transaction5下的任意class的任意方法|

          |第四个.. —— 通配 方法可以有0个或多个参数|

          ?

          • ?第五种:注解方式:

            ?

            <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="org.lgh.spring.transaction6" />      <tx:annotation-driven proxy-target-class ="true" transaction-manager="transactionManager"/>       <bean id="sessionFactory"                value="classpath:hibernate.cfg.xml" />            <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />      </bean>        <!-- 定义事务管理器(声明式的事务) -->        <bean id="transactionManager"          ref="sessionFactory" />      </bean>      <!-- 配置DAO -->      <bean id="userDao" ref="sessionFactory" />      </bean>        </beans>  
            ?

热点排行