2019-09-09 15:35:27 3332浏览
今天千锋扣丁学堂Java培训老师给大家分享一篇关于Spring事务管理方法步骤解析详解,文中通过示例代码介绍的非常详细,下面我们一起来看一下吧。
<?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/spring-context.xsd
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:property-placeholder location="classpath:db.properties" system-properties-mode="NEVER"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="service" class="com.test.tx.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<bean id="accountDao" class="com.test.tx.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--事务管理器-->
<bean id="manager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务-->
<aop:config>
<!--配置切入点,这里写自己想要使用Spring事务管理的类或接口,在上篇博客中有切入点配置方法-->
<aop:pointcut id="pointcut" expression="execution( * com.test.tx.service.IAccountService.*(..))"/>
<!--配置切面-->
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config>
<!--事务增强器-->
<tx:advice id="advice" transaction-manager="manager">
<tx:attributes>
<!--read-only可以将查询的方法设为只读事务-->
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
</beans>
<!--事务的注解驱动,注解解析器需要关联事务管理器--> <tx:annotation-driven transaction-manager="manager"/>
【关注微信公众号获取更多学习资料】 【扫码进入JavaEE/微服务VIP免费公开课】