標籤:spring-使用xml配置聲明式事務
一、建立spring項目
項目名稱:spring101501
二、在項目中添加spring支援
1.在項目中建立lib目錄
/lib
2.在lib目錄下添加jar包
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging.jar
junit-4.10.jar
log4j.jar
mysql-connector-java-5.1.18-bin.jar
spring-aop-3.2.0.RELEASE.jar
spring-aspects-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-jdbc-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
三、在項目中添加屬性檔案和設定檔
1.在項目中建立conf目錄
/conf
2.在conf目錄下添加屬性檔案
屬性檔案名稱:jdbc.properties
屬性檔案內容:
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/spring
jdbc.user = root
jdbc.password =root
3.在conf目錄下添加核心設定檔
設定檔名稱:applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
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">
</beans>
四、持久層設計
1.介面設計
在src目錄下建立介面所在的包
包名:cn.jbit.spring101501.dao
在包建立介面
介面名:AccountDao.java
介面內容:
public interface AccountDao {
public void outMoney(int outAccount,double money);
public void inMoney(int inAccount,double money);
}
2.實作類別設計
類名:AccountDaoImpl.java
類內容:
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public void inMoney(int inAccount, double money) {
String sql = "UPDATE Account SET money = money + ? WHERE id = ?";
this.getJdbcTemplate().update(sql,money,inAccount);
}
@Override
public void outMoney(int outAccount, double money) {
String sql = "UPDATE Account SET money = money - ? WHERE id = ?";
this.getJdbcTemplate().update(sql,money,outAccount);
}
}
五、業務層設計
1.業務介面設計
包名:cn.jbit.spring101501.service
介面名:AccountService.java
介面內容:
public interface AccountService {
public void transfer(int outAccount,int inAccount,double money);
}
2.業務實現設計
實作類別名:AccountServiceImpl.java
實作類別內容:
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
@Override
public void transfer(int outAccount, int inAccount, double money) {
accountDao.outMoney(outAccount, money);
int a = 1/0;
accountDao.inMoney(inAccount, money);
}
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public AccountDao getAccountDao() {
return accountDao;
}
}
六、在核心設定檔中添加相關配置
<!-- 1.載入屬性檔案 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.設定資料庫串連池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 3.配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4.配置交易管理員 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 5.配置DAO -->
<bean id="accountDao" class="cn.jbit.spring101501.dao.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!-- 6.配置Service -->
<bean id="accountService" class="cn.jbit.spring101501.service.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 7.定義通知(Advice) -->
<tx:advice id="accountAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 8.定義切面 -->
<aop:config>
<aop:pointcut expression="execution(* cn.jbit.spring101501.service.*ServiceImpl.*(..))" id="accountpintcut"/>
<aop:advisor advice-ref="accountAdvice" pointcut-ref="accountpintcut"/>
</aop:config>
七、測試
1.在項目中建立test目錄
/test
2.在test目錄下添加測試包
包名:cn.jbit.spring101501.service
3.在測試包下建立測試類別
類名:AccountServiceTest.java
類內容:
public class AccountServiceTest {
@Test
public void testTranser(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
AccountService accountService = (AccountService) context.getBean("accountService");
accountService.transfer(1, 2, 200);
}
}
本文出自 “素顏” 部落格,請務必保留此出處http://suyanzhu.blog.51cto.com/8050189/1564315
spring-使用XML配置聲明式事務