Spring事务xml版

博客展示了Spring框架下dao层和service层的代码,包含账户服务实现类及转账方法。还给出了xml配置文件,配置了数据源、事务管理器、事务通知等。最后提供了测试代码,用于测试账户转账功能,体现了Spring框架在后端开发中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

dao层

service层

package cn.itcast.day03.account;

public class AccountServiceImpl implements AccountService {

    private AccountDao ad;
    
    
    public void setAd(AccountDao ad) {
        this.ad = ad;
    }


    @Override
    public void transfer(Integer from, Integer to, double money) {
        ad.decreaceMoney(from, money);
        //int i=1/0;
        ad.increaceMoney(to, money);
        
    }

}

xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 指定Spring读取db.properties配置文件导入数据库链接信息 -->
<context:property-placeholder location="classpath:db.properties"/>

<!-- 将连接池放入spring容器 -->
<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>


<!-- 配置事务核心管理器 ,封装了所有事务操作,依赖于连接池-->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 事务模板对象,依赖于核心事务管理器 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManager"></property>
</bean>

<!-- 配置事务通知 -->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="transfer"  isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
    </tx:attributes>
</tx:advice>


<!-- 配置织入 -->
<aop:config>
    <!-- 切点 -->
    <aop:pointcut expression="execution(* cn.itcast.day03.account.*ServiceImpl.*(..))"  id="txpc"/>
    <!-- 切面 -->
    <aop:advisor advice-ref="myAdvice" pointcut-ref="txpc"/>
</aop:config>

 

 

<!-- Dao -->
<bean name="accountDao" class="cn.itcast.day03.account.AccountDaoImpl">
    <property name="dataSource"  ref="dataSource"></property>
</bean>

<!-- service -->
<bean name="accountService" class="cn.itcast.day03.account.AccountServiceImpl">
    <property name="ad" ref="accountDao"></property>
</bean>
</beans>

 

测试:

package cn.itcast.day03.account;

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:cn/itcast/day03/account/applicationContext.xml")
public class AccountTest {
    @Resource(name="accountService")
    private AccountService as;
    
@Test
    public void fun() {
        as.transfer(2, 1, 100d);
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值