**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 {
}