用事务通知声明式地管理事务
事务管理是一种横切关注点。为了在 Spring 2.x 中启用声明式事务管理,可以通过 tx Schema 中定义的 <tx:advice> 元素声明事务通知,为此必须事先将这个 Schema 定义添加到 <beans> 根元素中去。声明了事务通知后,就需要将它与切入点关联起来。由于事务通知是在 <aop:config> 元素外部声明的,所以它无法直接与切入点产生关联。所以必须在 <aop:config> 元素中声明一个增强器通知与切入点关联起来。由于 Spring AOP 是基于代理的方法,所以只能增强公共方法。因此,只有公有方法才能通过 Spring AOP 进行事务管理。
示例
<?xml version="1.0" encoding="UTF-8"?> <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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 导入资源文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置 C3P0 数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property> <property name="minPoolSize" value="${c3p0.minPoolSize}"></property> <property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property> <property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property> <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"></property> </bean> <!-- 配置 NamedParameterJdbcTemplate, 该对象可以使用具名参数, 其没有无参数的构造器, 所以必须为其构造器指定参数 --> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <constructor-arg ref="dataSource"></constructor-arg> </bean> <bean id="salaryDao" class="xyz.huning.spring4.transaction.xml.dao.impl.SalaryDao"> <property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"></property> </bean> <bean id="salaryService" class="xyz.huning.spring4.transaction.xml.service.impl.SalaryService"> <property name="salaryDao" ref="salaryDao"></property> </bean> <bean id="batchSalaryService" class="xyz.huning.spring4.transaction.xml.service.impl.BatchSalaryService"> <property name="salaryService" ref="salaryService"></property> </bean> <!-- 1. 配置事务管理器 --> <bean id="transactionMgr" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 2. 配置事务通知 --> <tx:advice id="salaryServiceTx" transaction-manager="transactionMgr"> <tx:attributes> <!-- 根据方法名指定事务的属性 --> <tx:method name="payoff" propagation="REQUIRES_NEW"/> <tx:method name="*"/><!--问题:如果不配置tx:method节点,事务异常时不回滚,不知道为什么?--> </tx:attributes> </tx:advice> <!-- 3. 配置事务通知绑定通知方法(即需要进行事务管理的方法): 3.1 配置事务切入点; 3.2 绑定事务切入点和事务通知 --> <aop:config> <aop:pointcut id="salaryServicePt" expression="execution(* xyz.huning.spring4.transaction.xml.service.impl.*.*(..))"/> <aop:advisor advice-ref="salaryServiceTx" pointcut-ref="salaryServicePt"/> </aop:config> </beans>
问题:
1. 如果从上文中删除下面的配置内容,系统发生异常后不会回滚,为什么?
<tx:attributes> <!-- 根据方法名指定事务的属性 --> <tx:method name="payoff" propagation="REQUIRES_NEW"/> <tx:method name="*"/><!--问题:如果不配置tx:method节点,事务异常时不回滚,不知道为什么?--> </tx:attributes>
2. 同一个类中嵌套事务,被调用的事务设置传播行为为Propagation.REQUIRES_NEW时不生效,发生异常时依然是全部回滚。为什么?
相关推荐
Spring5 框架 ---- 事务操作 ---- 代码 Spring5 框架 ---- 事务操作 ---- 代码 Spring5 框架 ---- 事务操作 ---- 代码 Spring5 框架 ---- 事务操作 ---- 代码 Spring5 框架 ---- 事务操作 ---- 代码 Spring5 框架 --...
spring4-mvc-gradle-xml-hello-world, Gradle + spring 4 MVC Hello World 示例( XML ) Gradle --spring-4 MVC Hello Worldspring 4 MVC + JSP视图+ XML配置模板,使用Gradle构建工具。使用的###1. 技术Gradle 2.0...
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
spring-mvc.xml spring-mybatis.xml web.xml log4j.properties,项目中需要用到的配置文件。直接可用。
spring.jar spring-aop.jar spring-aop.jar spring-beans.jar spring-hibernate3.jar spring-jdbc.jar spring-struts.jar spring-web.jar
使用Maven构建工具的Spring 3 MVC + JSP视图+ XML配置模板。 ### 1。 使用的技术 Maven的3 春天3.2.13。发布 JSTL 1.2 登入1.1.3 Boostrap 3 ### 2。 在本地运行该项目 $ git clone ...
NULL 博文链接:https://tonl.iteye.com/blog/2093314
spring整合hibernate实现事务处理 1.spring整合hibernate实现事务处理-注解方式 2.spring整合hibernate实现事务处理-XML方式
NULL 博文链接:https://tonl.iteye.com/blog/2093289
放在项目的resources下即可,用于配置日志打印在控制台的格式等信息,并且也可以调日志信息打印出来的颜色,还能够使日志信息记录在日志文件上并保存在项目根路径下,springboot则用此logback-spring.xml命名,若是maven...
第3种方式:使用拦截器 详见spring-core-transaction-3.xml 第4种方式:使用tx标签配置的拦截器 详见spring-core-transaction-4.xml 第5种方式:全注解 详见spring-core-transaction-5.xml
spring-boot spring-security-oauth2 完整demo,可以使用微信的方式来获取token和查看资源,注意看代码中的备注
propagation 其他没有事务的业务方法调用的时候!我们如何进行事务传递! 默认值:required:调用者有 我就有!调用者没有我自己开启一个事务 也有! dml SUPPORTS:调用者有 我就有!调用者没有我也不添加! 适用于查询...
shiro中的与spring结合的核心代码,可以使用缓存进行存储用户信息
演示了spring对aop的支持,包括注解方式、基于xml方式。
spring-webapp-no-xml 一个简单的 Spring webapp 项目,它使用纯 Java 配置(no-xml)演示 Spring 配置。建立战争mvn package - 构建一个 war 文件,例如target/spring-webapp-no-xml.war运行示例mvn tomcat7:run - ...
Spring如何整合Hibernate和struts --cglib来实现事务管理.rtf.rtf
spring-boot-mybaits-xml:xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简解决方案 spring-boot-mybatis-annotation-mulidatasource:springboot+mybatis(注解版)多数据源最简...
Spring IoC简单示例,使用XML配置依赖注入,使用Maven构建项目 Maven使用及Spring IoC教程参考我的Blog:http://blog.csdn.net/boyazuo