spring【6】(事物创建代理 BeanNameAutoProxyCreator)

9 篇文章 0 订阅
8 篇文章 0 订阅
<span style="font-size:18px;">public int delete(int sID) { 
  dbc = new DataBaseConnection(); 
  Connection con = dbc.getConnection(); 
  try { 
   con.setAutoCommit(false);// 更改JDBC事务的默认提交方式 
   dbc.executeUpdate("delete from xiao where ID=" + sID); 
   dbc.executeUpdate("delete from xiao_content where ID=" + sID); 
   dbc.executeUpdate("delete from xiao_affix where bylawid=" + sID); 
   con.commit();//提交JDBC事务 
   con.setAutoCommit(true);// 恢复JDBC事务的默认提交方式 
   dbc.close(); 
   return 1; 
  } 
  catch (Exception exc) { 
   con.rollBack();//回滚JDBC事务 
   exc.printStackTrace(); 
   dbc.close(); 
   return -1; 
  } 
} </span>

可以看到传统的java事务处理需要编写繁琐的JDBC代码,而基于springAOP的声明式事务让程序员告别JDBC事务代码

<span style="font-size:18px;">
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
            <list>
                <value>*Mgr</value>//事务处理应该做在事务层,详见spring【7】
                <value>*Service</value>
            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>transactionInterceptor</value>
            </list>
        </property>
    </bean></span>

BeanNameAutoProxyCreator是个根据bean名生成自动代理的代理创建器,该bean通常需要接受两个参数。第一个是beanNames属性,该属性用来设置哪些bean需要自动生成代理。另一个属性是interceptorNames,该属性则指定事务拦截器,自动创建事务代理时,系统会根据这些事务拦截器的属性来生成对应的事务代理。

beanNames:bean列表,这些bean会自动创建事务代理

interceptorNames:拦截器列表

<span style="font-size:18px;"><!-- add interceptor defined in  BeanNameAutoProxyCreator-->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <property name="driverClass">
            <value>${jdbc.driverClassName}</value>
        </property>
        <property name="jdbcUrl">
            <value>${jdbc.url}</value>
        </property>
        <property name="user">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
        <property name="minPoolSize">
            <value>5</value>
        </property>
        <property name="maxPoolSize">
            <value>30</value>
        </property>
        <property name="initialPoolSize">
            <value>10</value>
        </property>
        <property name="idleConnectionTestPeriod">
            <value>60</value>
        </property>
        <property name="maxIdleTime">
            <value>60</value>
        </property>
        <property name="acquireIncrement">
            <value>5</value>
        </property>
        <property name="maxStatements">
            <value>0</value>
        </property>
        <property name="checkoutTimeout">
            <value>1000</value>
        </property>
        <property name="acquireRetryAttempts">
            <value>30</value>
        </property>
        <property name="breakAfterAcquireFailure">
            <value>true</value>
        </property>
    </bean>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <!--<prop key="hibernate.hbm2ddl.auto">update</prop>-->
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <prop key="hibernate.enable_lazy_load_no_trans">${hibernate.enable_lazy_load_no_trans}</prop>
            </props>
        </property>
    </bean>

    <bean id="baseDao" abstract="true">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
    </bean>

    <bean id="transactionManager"
          class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <bean id="transactionInterceptor"
          class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager" ref="transactionManager"/>
        <property name="transactionAttributes">
            <props>
                <!--<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>-->
                <!--<prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>-->
                <!--<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>-->
                <!--<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>-->
                <!--<prop key="can*">PROPAGATION_REQUIRED,readOnly</prop>-->
                <!--<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>-->
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean></span>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值