posts - 325,  comments - 25,  trackbacks - 0
一.
使用TransactionProxyFactoryBean创建事务代理(通常事务代理以Service层为目标bean)
<bean id="personService" class="com.lin.personServiceImpl">
    <property name="personDao" ref="personDao"/>
</bean>
//配置hibernate的事务管理器,使用HibernateTransactionManager类,该类实现了PlatformTransactionManager接口,针对hibernate 持久化连接的特定实现
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
//配置personService bean的事务代理
<bean id="personServiceProxy"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            //指定事务管理器
    <property name="transactionManager" ref="transactionManager"/>
            //指定需要生成代理的日标bean
    <property name="persionService" ref="persionService"/>
            //指定事务属性
    <property name="transactionAttributes"
        <props>
            <prop key="insert*">PROPAGATION_REQUIRED,-MyCheckedException</prop>
            <prop key="update*>PROPAGATION_REQUIRED</prop>
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
        </props>
    </property>


二.使用自动创建代理简化事务配置
   使用BeanNameAutoProxyCreator 和DefaultAdvisorAutoProxyCreator创建代理时,并不一定是创建事务代理,关键在于传入的拦截器,如果传入事务拦截器,将可自动生成事务代理.
//使用jdbc局部事务策略
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
//配置目标bean1,该目标bean将由Bean后处理器自动生成代理
<bean id="testbean1" class="com.lin.Testbean1Impl">
    <property name="dataSource" ref="dataSource"/>
</bean
//配置目标bean2,该目标bean将由Bean后处理器自动生成代理
<bean id="testbean2" class="com.lin.Testbean2Impl">
    <property name="dataSource" ref="dataSource"/>
</bean
//配置事务拦截器bean
<bean id="transactionInterceptor"
   class="org.springframework.transaction.interceptor.TransactionInterceptor">
        //事务拦截器需要注入一个事务管理器
      <property name="transactionManager" ref="transactionManager"/>
       <property name="transactionAttributes">
            //定义事务传播属性
            <props>
                    <prop key="insert*">PROPAGATION_REQUIRED</prop>
                    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                    <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    //定义BeanNameAutoProxyCreator的Bean后处理器
 <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="beanNames">
        <list>
            <value>testbean1</value>
            <value>testbean2</value>
        </list>
            //此处可以增加其他需要创建事务代理的bean
    </property>
        //定义BeanNameAutoProxyCreator所需要的拦截器
     <property name="interceptorNames">
        <list>
            <value>transactionInterceptor</value>
                //此处可以增加其他新的Interceptor
        </list>
    </property>
 </bean>
    
posted on 2008-05-18 15:39 长春语林科技 阅读(765) 评论(0)  编辑  收藏 所属分类: spring

只有注册用户登录后才能发表评论。


网站导航:
 
<2008年5月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

 

长春语林科技欢迎您!

常用链接

留言簿(6)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

搜索

  •  

最新评论

阅读排行榜

评论排行榜