<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/springcontext.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/springaop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--扫描dao接口所在的包--> <context:component-scan base-package="com.aaa.spring.dao.impl,com.aaa.spring.service.impl"> </context:component-scan> <!---创建dbcp数据源连接池对象--> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> <property name="username" value="scott"></property> <property name="password" value="tiger"></property> </bean> <!--创建jdbcTemplate对象--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!---创建spring的事物管理器对象--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!---创建spring事物管理的通知类对象--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!---此处可以配置事物管理的一些特性--> <tx:attributes> <!--- name属性中可以使用通配符,用来指定该属性对哪些方法起作用 propagation 配置事物的传播特性 REQUIRED 判断当前方法是否存在事物,如果不存在事物就创建一个新的事物,如果存在事物就使用当前的事 物 REQUIRES_NEW 判断当前方法是否存在事物 ,如果不存在事物就创建一个新的事物, 如果存在事物,就把当前事物挂起,再启动一个新的事物 NESTED 嵌套事物 。判断当前方法是否存在事物,如果不存在事物就创建一个事物,如果存在事物,把当前事物 挂起, 再启动一个当前事物的子事物 。这样如果父事物产生了异常。子事物即使没有产生异常也会回滚。 read-only 配置事物的只读特性 true 当前事物是只读事物,在方法之后不会提交事物 flase 默认值 当前事物是非只读事物,在方法之后会提交事物 配置事物的只读特性 增删改方法一般需要事物,查询方法一般不需要事物,所以以后开发的时候最好查询方法把 read-only 设置成true,在一定程度上能提高程序的运行效率 --> <tx:method name="save*" propagation="NESTED" /> <tx:method name="select*" propagation="REQUIRED" read-only="true"></tx:method> </tx:attributes> </tx:advice> <!---配置spring的声明式事物--> <aop:config> <!---声明切入点对象 告诉spring 要对哪些类的哪些方法使用事物--> <aop:pointcut id="p1" expression="execution(* com.aaa.spring.service.impl.*.*(..))"></aop:pointcut> <!--声明事物管理专用的切面--> <aop:advisor advice-ref="txAdvice" pointcut-ref="p1"></aop:advisor> </aop:config> </beans>
原文:https://www.cnblogs.com/duguangming/p/10922434.html