springAOP对事务的处理(转账事务的处理)
1.导入相应的jar包
2.创建account数据库对账户信息进行存储
3.创建account类对数据库数据进行封装
public class account {
Integer id;
String name;
double 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 double getMoney ( ) {
return money;
}
public void setMoney ( double money ) {
this . money = money;
}
@Override
public String toString ( ) {
return "account [id=" + id + ", name=" + name + ", money=" + money + "]" ;
}
public account ( Integer id, String name, double money ) {
super ( ) ;
this . id = id;
this . name = name;
this . money = money;
}
public account ( ) {
super ( ) ;
}
}
4.创建accountDao接口和accountDaoImpl实现类实现账户金额的增减
public interface accountDao {
void addMoney ( Integer id, double money) ;
void subMoney ( Integer id, double money) ;
}
public class accountDaoImpl extends JdbcDaoSupport implements accountDao {
@Override
public void addMoney ( Integer id, double money ) {
String sql= "update account set money=money+? where id=?" ;
getJdbcTemplate ( ) . update ( sql, money, id) ;
}
@Override
public void subMoney ( Integer id, double money ) {
String sql= "update account set money=money-? where id=?" ;
getJdbcTemplate ( ) . update ( sql, money, id) ;
int i= 1 / 0 ;
}
}
5.创建accountService接口和accountServiceImpl实现类实现转账方法
public interface accountService {
void translate ( ) ;
}
public class accountDaoImpl extends JdbcDaoSupport implements accountDao {
@Override
public void addMoney ( Integer id, double money ) {
String sql= "update account set money=money+? where id=?" ;
getJdbcTemplate ( ) . update ( sql, money, id) ;
}
@Override
public void subMoney ( Integer id, double money ) {
String sql= "update account set money=money-? where id=?" ;
getJdbcTemplate ( ) . update ( sql, money, id) ;
int i= 1 / 0 ;
}
}
6.配置applicationContext.xml和db.properties文件
< ? 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 : aop= "http://www.springframework.org/schema/aop"
xmlns : tx= "http://www.springframework.org/schema/tx"
xmlns : context= "http://www.springframework.org/schema/context"
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- 4.3 . xsd
http : / / www. springframework. org/ schema/ aop http: / / www. springframework. org/ schema/ aop/ spring- aop- 4.3 . xsd
http : / / www. springframework. org/ schema/ tx http: / / www. springframework. org/ schema/ tx/ spring- tx- 4.3 . xsd">
< ! -- 配置datasource -- >
< context: property- placeholder location= "db.properties" / >
< bean name= "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>
< ! -- Dao -- >
< bean name= "accountDao" class = "dao.accountDaoImpl" >
< property name= "dataSource" ref= "datasource" > < / property>
< / bean>
< ! -- Service -- >
< bean name= "accountService" class = "service.accountServiceImpl" >
< property name= "dao" ref= "accountDao" > < / property>
< / bean>
< ! -- 配置事务核心管理器 -- >
< bean name= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name= "dataSource" ref= "datasource" > < / property>
< / bean>
< ! -- 事务通知 -- >
< tx: advice id= "txAdvice" transaction- manager= "transactionManager" >
< tx: attributes>
< ! -- 受到事务影响的方法 -- >
< tx: method name= "translate" isolation= "DEFAULT" read- only= "false" propagation= "REQUIRED" / >
< / tx: attributes>
< / tx: advice>
< aop: config>
< aop: pointcut expression= "execution(* service.*ServiceImpl.*(..))" id= "txPc" / >
< aop: advisor advice- ref= "txAdvice" pointcut- ref= "txPc" / >
< / aop: config>
< / beans>
jdbc. driverClass= com. mysql. jdbc. Driver
jdbc. jdbcUrl= jdbc: mysql: / / localhost: 3306 / spring
jdbc. user= root
jdbc. password= 612861288
7. 创建测试类
import javax. annotation. Resource;
import org. junit. Test;
import org. junit. runner. RunWith;
import org. springframework. test. context. ContextConfiguration;
import org. springframework. test. context. junit4. SpringJUnit4ClassRunner;
import service. accountService;
@RunWith ( SpringJUnit4ClassRunner. class)
@ContextConfiguration ( "classpath:applicationContext.xml" )
public class test {
@Resource ( name = "accountService" )
private accountService ac;
@Test
public void test01 ( ) {
ac. translate ( ) ;
}
}