spring【6】(事物创建代理 BeanNameAutoProxyCreator)
来源:互联网 发布:jdk windows x86 编辑:程序博客网 时间:2024/06/09 16:14
<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
- spring【6】(事物创建代理 BeanNameAutoProxyCreator)
- spring用BeanNameAutoProxyCreator自动创建事务代理
- spring用BeanNameAutoProxyCreator自动创建事务代理
- spring用BeanNameAutoProxyCreator自动创建事务代理
- spring用BeanNameAutoProxyCreator自动创建事务代理
- spring aop(八)--使用BeanNameAutoProxyCreator创建代理
- spring用BeanNameAutoProxyCreator自动创建事务代理
- spring aop 自动创建代理BeanNameAutoProxyCreator详解
- Spring-AOP 自动创建代理之BeanNameAutoProxyCreator
- 自动创建代理--BeanNameAutoProxyCreator
- spring(一) spring用BeanNameAutoProxyCreator自动创建事务代理
- spring事务管理(三)--用BeanNameAutoProxyCreator自动创建事务代理
- BeanNameAutoProxyCreator自动创建事务代理
- 用BeanNameAutoProxyCreator自动创建事务代理
- 用BeanNameAutoProxyCreator自动创建事务代理
- 用BeanNameAutoProxyCreator自动创建事务代理
- 用BeanNameAutoProxyCreator自动创建事务代理
- spring自动创建代理类DefaultAdvisorAutoProxyCreator和BeanNameAutoProxyCreator的声明顺序
- Linux运维细讲之vim的基础使用二
- javascript闭包 -- 懂不懂由你,反正我是懂了
- invalid use of incomplete type 'class QWidget'
- 置换TextView文字
- JavaScript高级程序设计(第3版)第三章读书笔记
- spring【6】(事物创建代理 BeanNameAutoProxyCreator)
- http://www.cnblogs.com/yjf512/p/3164400.html
- EFI基本概念之Timer
- Android Fragment 深度解析
- Apache+Tomcat负载均衡两种session共享方式的设置
- CodeBlocks Color Theme 主题修改
- Apache+Tomcat集群配置+session共享
- Android中Context的总结及其用法
- Xcode7.1 键盘通知突然出现的警告