动态代理——事务处理
生活随笔
收集整理的這篇文章主要介紹了
动态代理——事务处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
事務控制應該都是在業務層
連接的工具類,它用于從數據源中獲取一個連接,并且實現和線程的綁定
動態代理?
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.dym</groupId><artifactId>day03_eesy_01account</artifactId><version>1.0-SNAPSHOT</version><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.6</source><target>1.6</target></configuration></plugin></plugins></build><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>commons-dbutils</groupId><artifactId>commons-dbutils</artifactId><version>1.4</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies></project>Account.java
package com.itheima.domain;import java.io.Serializable;/*** 賬戶的實體類*/ public class Account implements Serializable {private Integer id;private String name;private Float money;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Float getMoney() {return money;}public void setMoney(Float money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + '\'' +", money=" + money +'}';} }ConnectionUtils.java
package com.itheima.utils;import javax.sql.DataSource; import java.sql.Connection;/*** 連接的工具類,它用于從數據源中獲取一個連接,并且實現和線程的綁定*/ public class ConnectionUtils {private ThreadLocal<Connection> tl = new ThreadLocal<Connection>();private DataSource dataSource;public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;}/*** 獲取當前線程上的連接* @return*/public Connection getThreadConnection() {try{//1.先從ThreadLocal上獲取Connection conn = tl.get();//2.判斷當前線程上是否有連接if (conn == null) {//3.從數據源中獲取一個連接,并且存入ThreadLocal中conn = dataSource.getConnection();tl.set(conn);}//4.返回當前線程上的連接return conn;}catch (Exception e){throw new RuntimeException(e);}}/*** 把連接和線程解綁*/public void removeConnection(){tl.remove();} }TransactionManager.java
package com.itheima.utils;/*** 和事務管理相關的工具類,它包含了,開啟事務,提交事務,回滾事務和釋放連接*/ public class TransactionManager {private ConnectionUtils connectionUtils;public void setConnectionUtils(ConnectionUtils connectionUtils) {this.connectionUtils = connectionUtils;}/*** 開啟事務*/public void beginTransaction(){try {connectionUtils.getThreadConnection().setAutoCommit(false);}catch (Exception e){e.printStackTrace();}}/*** 提交事務*/public void commit(){try {connectionUtils.getThreadConnection().commit();}catch (Exception e){e.printStackTrace();}}/*** 回滾事務*/public void rollback(){try {connectionUtils.getThreadConnection().rollback();}catch (Exception e){e.printStackTrace();}}/*** 釋放連接*/public void release(){try {connectionUtils.getThreadConnection().close();//還回連接池中connectionUtils.removeConnection();}catch (Exception e){e.printStackTrace();}} }IAccountDao.java
package com.itheima.dao;import com.itheima.domain.Account;import java.util.List;/*** 賬戶的持久層接口*/ public interface IAccountDao {/*** 查詢所有* @return*/List<Account> findAllAccount();/*** 查詢一個* @return*/Account findAccountById(Integer accountId);/*** 保存* @param account*/void saveAccount(Account account);/*** 更新* @param account*/void updateAccount(Account account);/*** 刪除* @param acccountId*/void deleteAccount(Integer acccountId);/*** 根據名稱查詢賬戶* @param accountName* @return 如果有唯一的一個結果就返回,如果沒有結果就返回null* 如果結果集超過一個就拋異常*/Account findAccountByName(String accountName); }AccountDaoImpl.java
package com.itheima.dao.impl;import com.itheima.dao.IAccountDao; import com.itheima.domain.Account; import com.itheima.utils.ConnectionUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler;import java.util.List;/*** 賬戶的持久層實現類*/ public class AccountDaoImpl implements IAccountDao {private QueryRunner runner;private ConnectionUtils connectionUtils;public void setRunner(QueryRunner runner) {this.runner = runner;}public void setConnectionUtils(ConnectionUtils connectionUtils) {this.connectionUtils = connectionUtils;}@Overridepublic List<Account> findAllAccount() {try{return runner.query(connectionUtils.getThreadConnection(),"select * from account",new BeanListHandler<Account>(Account.class));}catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic Account findAccountById(Integer accountId) {try{return runner.query(connectionUtils.getThreadConnection(),"select * from account where id = ? ",new BeanHandler<Account>(Account.class),accountId);}catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic void saveAccount(Account account) {try{runner.update(connectionUtils.getThreadConnection(),"insert into account(name,money)values(?,?)",account.getName(),account.getMoney());}catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic void updateAccount(Account account) {try{runner.update(connectionUtils.getThreadConnection(),"update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());}catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic void deleteAccount(Integer accountId) {try{runner.update(connectionUtils.getThreadConnection(),"delete from account where id=?",accountId);}catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic Account findAccountByName(String accountName) {try{List<Account> accounts = runner.query(connectionUtils.getThreadConnection(),"select * from account where name = ? ",new BeanListHandler<Account>(Account.class),accountName);if(accounts == null || accounts.size() == 0){return null;}if(accounts.size() > 1){throw new RuntimeException("結果集不唯一,數據有問題");}return accounts.get(0);}catch (Exception e) {throw new RuntimeException(e);}} }IAccountService.java
package com.itheima.service;import com.itheima.domain.Account;import java.util.List;/*** 賬戶的業務層接口*/ public interface IAccountService {/*** 查詢所有* @return*/List<Account> findAllAccount();/*** 查詢一個* @return*/Account findAccountById(Integer accountId);/*** 保存* @param account*/void saveAccount(Account account);/*** 更新* @param account*/void updateAccount(Account account);/*** 刪除* @param acccountId*/void deleteAccount(Integer acccountId);/*** 轉賬* @param sourceName 轉出賬戶名稱* @param targetName 轉入賬戶名稱* @param money 轉賬金額*/void transfer(String sourceName, String targetName, Float money);//void test();//它只是連接點,但不是切入點,因為沒有被增強 }AccountServiceImpl.java
package com.itheima.service.impl;import com.itheima.dao.IAccountDao; import com.itheima.domain.Account; import com.itheima.service.IAccountService;import java.util.List;/*** 賬戶的業務層實現類*/ public class AccountServiceImpl implements IAccountService{private IAccountDao accountDao;public void setAccountDao(IAccountDao accountDao) {this.accountDao = accountDao;}@Overridepublic List<Account> findAllAccount() {return accountDao.findAllAccount();}@Overridepublic Account findAccountById(Integer accountId) {return accountDao.findAccountById(accountId);}@Overridepublic void saveAccount(Account account) {accountDao.saveAccount(account);}@Overridepublic void updateAccount(Account account) {accountDao.updateAccount(account);}@Overridepublic void deleteAccount(Integer acccountId) {accountDao.deleteAccount(acccountId);}@Overridepublic void transfer(String sourceName, String targetName, Float money) {System.out.println("transfer....");//2.1 根據名稱查詢轉出賬戶Account source = accountDao.findAccountByName(sourceName);//2.2 根據名稱查詢轉入賬戶Account target = accountDao.findAccountByName(targetName);//2.3 轉出賬戶減錢source.setMoney(source.getMoney()-money);//2.4 轉入賬戶加錢target.setMoney(target.getMoney()+money);//2.5 更新轉出賬戶accountDao.updateAccount(source);//2.6 更新轉入賬戶accountDao.updateAccount(target);} }AccountServiceImpl_OLD.java
package com.itheima.service.impl;import com.itheima.dao.IAccountDao; import com.itheima.domain.Account; import com.itheima.service.IAccountService; import com.itheima.utils.TransactionManager;import java.util.List;/*** 賬戶的業務層實現類** 事務控制應該都是在業務層*/ public class AccountServiceImpl_OLD implements IAccountService{private IAccountDao accountDao;private TransactionManager txManager;public void setTxManager(TransactionManager txManager) {this.txManager = txManager;}public void setAccountDao(IAccountDao accountDao) {this.accountDao = accountDao;}@Overridepublic List<Account> findAllAccount() {try {//1.開啟事務txManager.beginTransaction();//2.執行操作List<Account> accounts = accountDao.findAllAccount();//3.提交事務txManager.commit();//4.返回結果return accounts;}catch (Exception e){//5.回滾操作txManager.rollback();throw new RuntimeException(e);}finally {//6.釋放連接txManager.release();}}@Overridepublic Account findAccountById(Integer accountId) {try {//1.開啟事務txManager.beginTransaction();//2.執行操作Account account = accountDao.findAccountById(accountId);//3.提交事務txManager.commit();//4.返回結果return account;}catch (Exception e){//5.回滾操作txManager.rollback();throw new RuntimeException(e);}finally {//6.釋放連接txManager.release();}}@Overridepublic void saveAccount(Account account) {try {//1.開啟事務txManager.beginTransaction();//2.執行操作accountDao.saveAccount(account);//3.提交事務txManager.commit();}catch (Exception e){//4.回滾操作txManager.rollback();}finally {//5.釋放連接txManager.release();}}@Overridepublic void updateAccount(Account account) {try {//1.開啟事務txManager.beginTransaction();//2.執行操作accountDao.updateAccount(account);//3.提交事務txManager.commit();}catch (Exception e){//4.回滾操作txManager.rollback();}finally {//5.釋放連接txManager.release();}}@Overridepublic void deleteAccount(Integer acccountId) {try {//1.開啟事務txManager.beginTransaction();//2.執行操作accountDao.deleteAccount(acccountId);//3.提交事務txManager.commit();}catch (Exception e){//4.回滾操作txManager.rollback();}finally {//5.釋放連接txManager.release();}}@Overridepublic void transfer(String sourceName, String targetName, Float money) {try {//1.開啟事務txManager.beginTransaction();//2.執行操作//2.1根據名稱查詢轉出賬戶Account source = accountDao.findAccountByName(sourceName);//2.2根據名稱查詢轉入賬戶Account target = accountDao.findAccountByName(targetName);//2.3轉出賬戶減錢source.setMoney(source.getMoney()-money);//2.4轉入賬戶加錢target.setMoney(target.getMoney()+money);//2.5更新轉出賬戶accountDao.updateAccount(source);int i=1/0;//2.6更新轉入賬戶accountDao.updateAccount(target);//3.提交事務txManager.commit();}catch (Exception e){//4.回滾操作txManager.rollback();e.printStackTrace();}finally {//5.釋放連接txManager.release();}} }BeanFactory.java
package com.itheima.factory;import com.itheima.service.IAccountService; import com.itheima.utils.TransactionManager;import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy;/*** 用于創建Service的代理對象的工廠*/ public class BeanFactory {private IAccountService accountService;private TransactionManager txManager;public void setTxManager(TransactionManager txManager) {this.txManager = txManager;}public final void setAccountService(IAccountService accountService) {this.accountService = accountService;}/*** 獲取Service代理對象* @return*/public IAccountService getAccountService() {return (IAccountService)Proxy.newProxyInstance(accountService.getClass().getClassLoader(),accountService.getClass().getInterfaces(),new InvocationHandler() {/*** 添加事務的支持** @param proxy* @param method* @param args* @return* @throws Throwable*/@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {if("test".equals(method.getName())){return method.invoke(accountService,args);}Object rtValue = null;try {//1.開啟事務txManager.beginTransaction();//2.執行操作rtValue = method.invoke(accountService, args);//3.提交事務txManager.commit();//4.返回結果return rtValue;} catch (Exception e) {//5.回滾操作txManager.rollback();throw new RuntimeException(e);} finally {//6.釋放連接txManager.release();}}});} }bean.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--配置代理的service--><bean id="proxyAccountService" factory-bean="beanFactory" factory-method="getAccountService"></bean><!--配置beanfactory--><bean id="beanFactory" class="com.itheima.factory.BeanFactory"><!-- 注入service --><property name="accountService" ref="accountService"></property><!-- 注入事務管理器 --><property name="txManager" ref="txManager"></property></bean><!-- 配置Service --><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"><!-- 注入dao --><property name="accountDao" ref="accountDao"></property></bean><!--配置Dao對象--><bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"><!-- 注入QueryRunner --><property name="runner" ref="runner"></property><!-- 注入ConnectionUtils --><property name="connectionUtils" ref="connectionUtils"></property></bean><!--配置QueryRunner--><bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"></bean><!-- 配置數據源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><!--連接數據庫的必備信息--><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy"></property><property name="user" value="root"></property><property name="password" value="1234"></property></bean><!-- 配置Connection的工具類 ConnectionUtils --><bean id="connectionUtils" class="com.itheima.utils.ConnectionUtils"><!-- 注入數據源--><property name="dataSource" ref="dataSource"></property></bean><!-- 配置事務管理器--><bean id="txManager" class="com.itheima.utils.TransactionManager"><!-- 注入ConnectionUtils --><property name="connectionUtils" ref="connectionUtils"></property></bean> </beans>AccountServiceTest.java
package com.itheima.test;import com.itheima.service.IAccountService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** 使用Junit單元測試:測試我們的配置*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:bean.xml") public class AccountServiceTest {@Autowired@Qualifier("proxyAccountService")private IAccountService as;@Testpublic void testTransfer(){as.transfer("aaa","bbb",100f);}}總結
以上是生活随笔為你收集整理的动态代理——事务处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于子类的动态代理
- 下一篇: 动态代理——》AOP —— Spring