Spring(八)事务管理
一、事务管理-aop
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 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/spring-context.xs http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--扫描注释--> <context:component-scan base-package="com.icss"></context:component-scan> <!--使用spring的配置自动完成创建代理织入切面的工作。 通过aop命名空间的<aop:aspectj-autoproxy/> 声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!--引入.properties文件--> <context:property-placeholder location="jdbc.properties"></context:property-placeholder> <!--数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${driverClass}"></property> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="user" value="${name}"></property> <property name="password" value="${password}"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!--事务管理控制,事务管理器--> <bean id="tranManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--将数据源代入--> <property name="dataSource" ref="dataSource"></property> </bean> <!--将数据源管理部分代入--> <tx:advice id="txadvice" transaction-manager="tranManager"> <tx:attributes>
              <!--isolation="DEFAULT" 隔离级别(解决脏读、幻读、重复读)
              propagation="REQUIRED"  传播策略
              read-only="false" 读写方式-->
<tx:method name="test" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/> </tx:attributes> </tx:advice> <aop:config> <!--设置切入点--> <aop:pointcut id="px1" expression="execution( * com.icss.jdbc.service.impl.UserServiceImpl.test(..))"/> <aop:advisor advice-ref="txadvice" pointcut-ref="px1"></aop:advisor> </aop:config> </beans>
二、事务管理控制--@Transactional:
public class UserServiceImpl implements UserService { @Autowired UserDao dao; @Override @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false) public void test(){dao.test();} }
.xml文件
<!--事务管理控制--> <bean id="tranManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--将数据源代入--> <property name="dataSource" ref="dataSource"></property> </bean> <!--事务控制管理器--> <tx:annotation-driven transaction-manager="tranManager"></tx:annotation-driven>
                    
                
                
            
        
浙公网安备 33010602011771号