标签:val direct ioc url value app upd service rect
转账案例环境搭建
1.引入JAR包
IOC的6个包
AOP的4个包
C3P0的1个包
MySQL的1个驱动包
JDBC的2个目标包
整合JUnit测试1个包
2.引入配置文件
log4j.properties+applicationContext.xml
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ### log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=c\:mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change ‘info‘ to ‘debug‘ ### log4j.rootLogger=info, stdout
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- C3P0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql:///spring_day03"/> <property name="user" value="root"/> <property name="password" value="toor"/> </bean> </beans>
3.创建对应的包结构
public interface AccountDao { /** * 转出 * @param out * @param money */ public void outMoney(String out,double money); /** * 转入 * @param in * @param money */ public void inMoney(String in,double money); }
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { //转出 @Override public void outMoney(String out, double money) { // TODO Auto-generated method stub this.getJdbcTemplate().update("update t_account set money=money-? where name=?", money,out); } //转入 @Override public void inMoney(String in, double money) { // TODO Auto-generated method stub this.getJdbcTemplate().update("update t_account set money=money+? where name=?", money,in); } }
public interface AccountService { /** * 转账 * @param out * @param in * @param money */ public void pay(String out,String in,double money); }
public class AccountServiceImpl implements AccountService { private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override public void pay(String out, String in, double money) { // TODO Auto-generated method stub //转出 accountDao.outMoney(out, money); //异常 //int i = 1/0; //转入 accountDao.inMoney(in, money); } }
4.修改配置文件
<!-- 管理业务层 注入持久层对象--> <bean id="accountService" class="com.spring.demo1.AccountServiceImpl"> <property name="accountDao" ref="accountDao"/> </bean> <!-- 管理持久层 注入数据源(由于继承JdbcDaoSupport,自动生成JDBC模板类)--> <bean id="accountDao" class="com.spring.demo1.AccountDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean>
5.测试
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value="classpath:applicationContext.xml") public class Demo1 { @Resource(name="accountService") private AccountService accountService; @Test public void m01(){ accountService.pay("测试1", "测试2", 1000); } }
不出现异常正常转账
出现异常不回滚
1.XML方式
1.配置事务管理器
<!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
2.配置事务增强
<!-- 配置事务增强 --> <tx:advice id="myAdvice" transaction-manager="transactionManager"> <tx:attributes > <tx:method name="pay"/> </tx:attributes> </tx:advice>
name="pay" 代表切入点
4.配置AOP切面
<!-- 配置AOP切面 --> <aop:config> <aop:advisor advice-ref="myAdvice" pointcut="execution(* com.spring.demo2.AccountServiceImpl.pay(..))"/> </aop:config>
注意:如果是自己编写的切面,使用<aop:aspect>标签,如果是系统制作的,使用<aop:advisor>标签。
5.测试
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value="classpath:applicationContext2.xml") public class Demo1 { @Resource(name="accountService") private AccountService accountService; @Test public void m01(){ accountService.pay("测试1", "测试2", 1000); } }
不出现异常正常转账
出现异常回滚
2.注解方式
1.配置事务管理器
<!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
2.开启注解事务
<!-- 开启注解事务 --> <tx:annotation-driven transaction-manager="transactionManager"/>
3.添加注解
在业务层上添加一个注解:@Transactiona
4.测试
不出现异常正常转账
出现异常回滚
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value="classpath:applicationContext3.xml") public class Demo1 { @Resource(name="accountService") private AccountService accountService; @Test public void m01(){ accountService.pay("测试1", "测试2", 1000); } }
标签:val direct ioc url value app upd service rect
原文地址:http://www.cnblogs.com/NEWHOM/p/6822447.html