AOP相关概念

AOP简介

简要的说它就是把我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的技术上,对我们原有的方法进行增强,以减少重复代码,提高开发效率,维护方便

AOP实现技术

使用动态代理技术

AOP的具体应用

案例中的问题

package com.test.dao.impl;


import com.test.dao.IAccountDao;
import com.test.domain.Account;
import com.test.utils.ConnectionUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 账户的持久层实现类
 */
@Repository("accountDao")
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;
    }

    @Override
    public List<Account> findAllAccount() {
   
        try{
   
            return runner.query(connectionUtils.getThreadConnection(),"select * from account",new BeanListHandler<Account>(Account.class));
        }catch (Exception e) {
   
            throw new RuntimeException(e);
        }
    }

    @Override
    public 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);
        }
    }

    @Override
    public 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);
        }
    }

    @Override
    public 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);
        }
    }

    @Override
    public void deleteAccount(Integer accountId) {
   
        try{
   
            runner.update(connectionUtils.getThreadConnection(),"delete from account where id=?",accountId);
        }catch (Exception e) {
   
            throw new RuntimeException(e);
        }
    }

    @Override
    public 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);
        }
    }
}

package com.test.service.impl;

import com.test.dao.IAccountDao;
import com.test.domain.Account;
import com.test.service.IAccountService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 账户的业务层实现类
 */
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
   
    private IAccountDao accountDao;

    public void setAccountDao(IAccountDao accountDao) {
   
        this.accountDao = accountDao;
    }

    @Override
    public List<Account> findAllAccount() {
   
        return accountDao.findAllAccount();
    }

    @Override
    public Account findAccountById(Integer accountId) {
   
        return accountDao.findAccountById(accountId);

    }

    @Override
    public void saveAccount(Account account) {
   
        accountDao.saveAccount(account);
    }

    @Override
    public void updateAccount(Account account) {
   
        accountDao.updateAccount(account);
    }

    @Override
    public void deleteAccount(Integer acccountId) {
   
        accountDao.deleteAccount(acccountId);
    }

    @Override
    public 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);

//            int i=1/0;//模拟账号异常

        //2.6更新转入账户
        accountDao.updateAccount(target);
    }
}

当我们执行时,由于执行异常,转账失败。因为每次执行持久层方法都是独立事务,导致无法实现事务控制

问题解决

让业务层来控制事务的提交和回滚

package com.test.service.impl;

import com.test.dao.IAccountDao;
import com.test.domain.Account;
import com.test.service.IAccountService;
import com.test.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;
    }

    @Override
    public List<Account> findAllAccount() {
   
        try {
   
            //1.开启事务
            txManager.beginTransaction(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值