SpringAOP

**2021.4.14SpringAOP
以SpringAOP为例讲了转账功能,有四种实现方式
–编程式
/

@Description:转账案例的DAO层接口
/
public interface AccountDao {
/*

  • @param out
  • :转出账号
  • @param money
  • :转账金额
    /
    public void outMoney(String out, Double money);
    在这里插入图片描述
    }
    /
    *

@Description:转账案例的DAO层实现类
/
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
/*
@param out

在这里插入图片描述
@param money在这里插入图片描述
/
@Override
public void outMoney(String out, Double money) {
String sql = “update account set money = money-? where name = ?”;
this.getJdbcTemplate().update(sql, money, out);
}
/*
@param in
在这里插入图片描述
@param money在这里插入图片描述
/
@Override
public void inMoney(String in, Double money) {
String sql = “update account set money = money+? where name = ?”;
this.getJdbcTemplate().update(sql, money, in);
}
}
/*
@Description:转账案例的业务接口
/
public interface AccountService {
/*

  • @param out :转出账号
  • @param in :转入账号
  • @param money :转账金额
    /
    public void transfer(String out,String in,Double money);
    }
    /*

@Description:转账案例的业务层实现类
*/
public class AccountServiceImpl implements AccountService {
// 注入转账的DAO
private AccountDao accountDao;

// 注入事务管理的模板
private TransactionTemplate transactionTemplate;

/**

@param out
在这里插入图片描述
@param in在这里插入图片描述
@param money在这里插入图片描述
*/
@Override
public void transfer(final String out, final String in, final Double money) {在这里插入图片描述
}

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

public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
}
applicationContext1.xml
在这里插入图片描述
测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(“classpath:applicationContext1.xml”)
public class TransactionTest {
@Resource(name = “accountService”)
private AccountService accountService;
在这里插入图片描述
}
–基于TransactionProxyFactoryBean的方式

public class AccountServiceImpl implements AccountService {
// 注入转账的DAO
private AccountDao accountDao;在这里插入图片描述
}
applicationContext2.xml在这里插入图片描述
测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(“classpath:applicationContext2.xml”)
public class TransactionTest {
/**

  • 一定要注入代理类:因为代理类进行增强的操作
    */
    // @Resource(name=“accountService”)
    @Resource(name = “accountServiceProxy”)
    private AccountService accountService;
    在这里插入图片描述
    }
    –基于XML配置

public class AccountServiceImpl implements AccountService {
// 注入转账的DAO
private AccountDao accountDao;

/**
}
applicationContext3.xml在这里插入图片描述

测试:

/**

@Description:Spring的声明式事务管理的方式二:基于AspectJ的XML方式的配置
/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(“classpath:applicationContext3.xml”)
public class TransactionTest {
/*

一定要注入代理类:因为代理类进行增强的操作
*/
@Resource(name = “accountService”)
private AccountService accountService;
@Test
public void demo1() {
accountService.transfer(“aaa”, “bbb”, 200d);
}
}
–基于注解

/**

@Transactional中的的属性 propagation :事务的传播行为 isolation :事务的隔离级别 readOnly :只读
在这里插入图片描述
在这里插入图片描述
*/
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false)
public class AccountServiceImpl implements AccountService {
// 注入转账的DAO
private AccountDao accountDao;
在这里插入图片描述
}

applicationContext4.xml在这里插入图片描述
测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(“classpath:applicationContext4.xml”)
public class TransactionTest {在这里插入图片描述
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值