使用aop解决事务问题(xml版)
生活随笔
收集整理的這篇文章主要介紹了
使用aop解决事务问题(xml版)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、引入依賴
pom.xml代碼:
二、目錄結(jié)構(gòu)
三、applicationContext.xml配置文件
<?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"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"><!--開啟注解掃描--><context:component-scan base-package="com.william"></context:component-scan><!--創(chuàng)建QueryRunner--><bean id="QueryRunner" class="org.apache.commons.dbutils.QueryRunner"></bean><!--創(chuàng)建dataSource--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property></bean><!--引入屬性文件--><context:property-placeholder location="classpath:db.properties"></context:property-placeholder><!--使用aop解決事務(wù)問題--><!--配置切面:切入點(diǎn),增強(qiáng)--><!--增強(qiáng):事務(wù)管理--><bean id="transactionManager" class="com.william.utils.TransactionManager"></bean><!--aop配置--><aop:config><!--配置切面: ref 關(guān)聯(lián)增強(qiáng)對(duì)象--><aop:aspect ref="transactionManager"><!--配置切入點(diǎn)--><aop:pointcut id="pc" expression="execution(* com.william.service.Impl.*.*(..))"></aop:pointcut><!--織入--><aop:before method="beganTransaction" pointcut-ref="pc"></aop:before><aop:after-returning method="Commit" pointcut-ref="pc"></aop:after-returning><aop:after-throwing method="rollBack" pointcut-ref="pc"></aop:after-throwing><aop:after method="release" pointcut-ref="pc"></aop:after></aop:aspect></aop:config> </beans>四、Class文件
1.AccountDaoImpl
代碼:
package com.william.Dao.Impl;import com.william.Dao.AccountDao; import com.william.domain.Account; import com.william.utils.ConnectionUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository;import java.sql.SQLException; import java.util.List;/*** @author :lijunxuan* @date :Created in 2019/5/27 16:00* @description :* @version: 1.0*/ @Repository public class AccountDaoImpl implements AccountDao {@AutowiredConnectionUtils connectionUtils;@AutowiredQueryRunner queryRunner;@Overridepublic Account findByUserName(String username ) {String sql="select * from account where name = ?";try {return queryRunner.query(connectionUtils.getThreadConnection(),sql,new BeanHandler<>(Account.class),username);} catch (SQLException e) {e.printStackTrace();}return null;}@Overridepublic void Update(Account account) {String sql =" update account set money = ? where id =?";Object [] params= {account.getMoney(),account.getId()};try {queryRunner.update(connectionUtils.getThreadConnection(),sql,params);} catch (SQLException e) {e.printStackTrace();}} }2.AccountServiceImpl
package com.william.service.Impl;import com.william.Dao.AccountDao; import com.william.domain.Account; import com.william.service.AccountService; import com.william.utils.TransactionManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;/*** @author :lijunxuan* @date :Created in 2019/5/27 16:14* @description :* @version: 1.0*/ @Service public class AccountServiceImpl implements AccountService {@AutowiredAccountDao accountDao;@Overridepublic void transfer(String FromMoney, String ToMoney, Float money) {//開啟事務(wù):transactionManager//查詢賬戶名稱 兩個(gè)賬戶名稱Account FromuserName = accountDao.findByUserName(FromMoney);Account ToUserName = accountDao.findByUserName(ToMoney);//修改金額FromuserName.setMoney(FromuserName.getMoney()-money);ToUserName.setMoney(ToUserName.getMoney()+money);//int i=1/0;//修改賬戶表accountDao.Update(FromuserName);accountDao.Update(ToUserName);} }3.Utils工具類
(1)ConnectionUtils
package com.william.utils;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException;/*** @author :lijunxuan* @date :Created in 2019/5/27 16:50* @description :* @version: 1.0*/ @Component public class ConnectionUtils {private ThreadLocal<Connection> t1=new ThreadLocal();@AutowiredDataSource dataSource;/*** 用來獲取線程中連接對(duì)象* 1, 在第一次訪問該方法時(shí), 線程中沒有對(duì)象,先獲取一個(gè),放入線程*/public Connection getThreadConnection(){//獲取線程池中的對(duì)象Connection conn = t1.get();if (conn==null){try {conn = dataSource.getConnection();t1.set(conn);} catch (SQLException e) {e.printStackTrace();}}return conn;}/*** 移除線程*/public void remove(){t1.remove();} }(2)TransactionManager
package com.william.utils;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import java.sql.SQLException;/*** @author :lijunxuan* @date :Created in 2019/5/27 16:50* @description :* @version: 1.0*/ @Component public class TransactionManager {@AutowiredConnectionUtils connectionUtils;//開啟事務(wù)public void beganTransaction(){try {connectionUtils.getThreadConnection().setAutoCommit(false);} catch (SQLException e) {e.printStackTrace();}}//提交事務(wù)public void Commit(){try {connectionUtils.getThreadConnection().commit();} catch (SQLException e) {e.printStackTrace();}}//回滾public void rollBack(){try {connectionUtils.getThreadConnection().rollback();} catch (SQLException e) {e.printStackTrace();}}//釋放資源public void release(){try {connectionUtils.getThreadConnection().setAutoCommit(true);connectionUtils.getThreadConnection().close();connectionUtils.remove();} catch (SQLException e) {e.printStackTrace();}} }4.TestTrasfer測(cè)試類
package com.william;import com.william.service.AccountService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** @author :lijunxuan* @date :Created in 2019/5/27 16:22* @description :* @version: 1.0*/ @Component @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class TestTrasfer {@AutowiredAccountService accountService;@Testpublic void testTrasferMoney(){accountService.transfer("william","william-Li",500f);} } 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的使用aop解决事务问题(xml版)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java模拟器(java模拟ddos)
- 下一篇: 使用注解版AOP解决事务问题