spring【6】(事物创建代理 BeanNameAutoProxyCreator)
2016-02-27 00:05
549 查看
<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>
相关文章推荐
- Cannot change version ...Module to 3.0 解决
- spring security oauth2
- spring项目整合mongo
- 第10课:底实战详解使用Java开发Spark程序学习笔记
- spring【7】(spring事务详解)
- NIO总结
- Java [Leetcode 136]Single Number
- struts-config.xml文件详解
- 正则表达式(一)
- 类加载机制
- JAVA常用基础知识点[继承,抽象,接口,静态,枚举,反射,泛型,多线程.]
- Spring MVC 无XML配置入门示例
- spring框架中使用junit单元测试
- Struts2防止表单重复提交
- java中HashMap详解
- java基础(函数)
- Spring Boot Junit单元测试
- Java 8 Lambda Expression 学习
- Java高级--JDBC
- javadoc命令