一、简单实现基本业务
1、new一个web project
2、加入必备的jar包(如下图)、配置log4j.properties、db.properties文件
3、数据库,数据表准备
4、创建一个AccountDao接口和其实现类AccountDaoImpl
AccountDao.java
package com.hy.spring.dao;
public interface AccountDao {
void addMoney(Integer id, double money);
void subMoney(Integer id, double money);
}
AccountDaoImpl.java
package com.hy.spring.dao.impl;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.hy.spring.dao.AccountDao;
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao{
@Resource(name="jdbcTemplate")
private JdbcTemplate jtemplate;
@Override
public void addMoney(Integer id, double money) {
String sql = "update account set acccount = acccount + ? where id = ?";
jtemplate.update(sql,money,id);
}
@Override
public void subMoney(Integer id, double money) {
String sql = "update account set acccount = acccount - ? where id = ?";
jtemplate.update(sql,money,id);
}
}
5、创建Service
AccountService.java
package com.hy.spring.service;
public interface AccountService {
public void transfer(Integer from, Integer to, double money);
}
AccountServiceImpl.java
package com.hy.spring.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.hy.spring.dao.AccountDao;
import com.hy.spring.service.AccountService;
@Service("accountService")
public class AccountServiceImpl implements AccountService{
@Resource(name="accountDao")
private AccountDao accountDao;
@Override
public void transfer(Integer from, Integer to, double money) {
accountDao.subMoney(from, money);
accountDao.addMoney(to, money);
}
}
6、配置applicationContex.xml
<?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:p="http://www.springframework.org/schema/p"
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-4.0.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">
<!-- 使用注解方式 -->
<context:component-scan base-package="com.hy.spring.dao"></context:component-scan>
<context:component-scan base-package="com.hy.spring.service"></context:component-scan>
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
</bean>
<!-- JdbcTemplate配置 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务管理器 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
7、添加测试用例
AccountTest.java
package com.hy.spring.service;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AccountTest {
@Resource(name="accountService")
private AccountService accountService;
@Test
public void test() {
accountService.transfer(1, 2, 10000.02);
}
}
二、优化
出现的问题:
如果我们在AccountServiceImpl.java中的subMoney和addMoney之间加入一个异常操作,例如:int a = 10/0;,发现上述案例的转账方钱变少了,但是被转账账户的钱没有增加。
优化
添加事务处理,在applicationContext.xml中配置事务处理,添加如下代码,java代码不用修改就能实现对这个异常的处理
<!-- 配置事务管理器 -->
<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="transfer" propagation="REQUIRED"/>
<!-- 常用举例,本题暂时未用到以下情况 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="drop*" propagation="REQUIRED"/>
<tx:method name="search*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 将通知织入切入点 -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut expression="execution(public void com.hy.spring.service.impl.AccountServiceImpl.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
总结:
1、还需要深入理解AOP,
2、注解方式和xml文件配置方式使用还是不熟练,
3、对Spring的框架还需要深入了解