Spring:事务管理-阶段随笔+总结()

目录

目标:XML实现声明式事务管理:简单转账方法

Dao层

        Dao

        DaoImpl

Service层

        Service

        ServiceImpl

XML配置

Test

报错:No bean named 'ServiceImpl' available(已解决)

补充:注解方式进行Spring声明式事务管理

Dao层

Service层

XML配置

Test

运行结果


目标:XML实现声明式事务管理:简单转账方法

Dao层

        Dao

package programming.dao;

public interface Dao {
    void out(String outer, int money);

    void in(String inner, int money);
}

        DaoImpl

package programming.dao;
import org.springframework.jdbc.core.JdbcTemplate;
public class DaoImpl implements Dao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    @Override
    public void out(String outer, int money) {
        this.jdbcTemplate.update("UPDATE account SET money = money - ? WHERE NAME = ?", money, outer);
    }
    @Override
    public void in(String inner, int money) {
        this.jdbcTemplate.update("UPDATE account SET money = money + ? WHERE NAME = ?", money, inner);
    }
}

Service层

        Service

package programming.service;
import java.sql.SQLException;
public interface Service {
    void transfer(String outer, String inner, int money) throws SQLException;
}

        ServiceImpl

package programming.service;
import programming.dao.DaoImpl;
public class ServiceImpl implements Service {
    private DaoImpl dao;
    public void setDao(DaoImpl dao) {
        this.dao = dao;
    }

    @Override
    public void transfer(String outer, String inner, int money){
            dao.out(outer, money);
            System.out.println("----转账人已转出----");

            System.out.println(1 / 0);

            dao.in(inner, money);
            System.out.println("----收账人已转入----");

    }
}

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/beans
        http://www.springframework.org/schema/beans/spring-beans-3.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">
<!--数据库连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <!--url中:3360表示找到端口 demo1表示选择该数据库-->
        <property name="url" value="jdbc:mysql://localhost:3306/demo1"></property>
        <property name="username" value="root"></property>
        <property name="password" value="207863"></property>
        <!--驱动程序的类注入-->
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
    </bean>


<!--jdbc模板 注入dataSource连接池-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
    </bean>

<!--在Dao层注入jdbc模板类-->
    <bean id="daoImpl" class="programming.dao.DaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
<!--在Service层注入Dao层-->
    <bean id="serviceImpl" class="programming.service.ServiceImpl">
        <property name="dao" ref="daoImpl"></property>
    </bean>

    <!--配置事务管理器-->
    <bean id="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="trans*"/><!--相当于一个过滤器-->
        </tx:attributes>
    </tx:advice>

    <!--配置切点和切入面-->
    <aop:config>
        <!--切入点-->
        <aop:pointcut id="pt" expression="execution(* programming.service.ServiceImpl.*(..))"/>  <!--expression:获取切点的检索路径,为方法全路径名-->
        <!--切面-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>    <!--  advice-ref:通知的id  pointcut-ref:切点的id  -->
    </aop:config>





</beans>

Test

package programming;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import programming.service.Service;
import programming.service.ServiceImpl;

import java.sql.SQLException;

public class TestDB {

    @Test
    public void TestAccount() throws SQLException {
        ApplicationContext context = new ClassPathXmlApplicationContext("programming.xml");
        Service service = (Service) context.getBean("ServiceImpl");
        service.transfer("A", "B", 100);
    }
}
//问题:xml中切面路径写对了,但是找不到已注入的bean

报错:No bean named 'ServiceImpl' available(已解决)

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'ServiceImpl' available

@Test
public void TestAccount() throws SQLException {
    ApplicationContext context = new ClassPathXmlApplicationContext("programming.xml");
    Service service = (Service) context.getBean("serviceImpl");
    service.transfer("A", "B", 100);
}

 问题关键:getBean("类的首字母小写"),和XML配置注入中的"id"是一样的!!!

补充:注解方式进行Spring声明式事务管理

DAO和Service层的接口时一样的,不做调整。

Dao层

AccountDaoImpl

package annotation.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void out(String outer, int money) {
        this.jdbcTemplate.update("UPDATE account SET money = money - ? WHERE NAME = ?", money, outer);
    }

    @Override
    public void in(String inner, int money) {
        this.jdbcTemplate.update("UPDATE account SET money = money + ? WHERE NAME = ?", money, inner);
    }
}

Service层

AccountServiceImpl

package annotation.service;

import annotation.dao.AccountDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional(readOnly = false,timeout = -1,propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ) // 事务注解,类上面或者里面的方法上添加注解
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDaoImpl dao;
    @Override
    public void transfer(String outer, String inner, int money) {
        dao.out(outer, money);
        System.out.println("----转账人已转出----");

        dao.in(inner, money);
        System.out.println("----收账人已转入----");

    }
}

XML配置

annotation.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--开启对象注解扫描,开启Transaction注解扫描-->
    <context:component-scan base-package="annotation"/>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!--数据库连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <!--url中:3360表示找到端口 demo1表示选择该数据库-->
        <property name="url" value="jdbc:mysql://localhost:3306/demo1"></property>
        <property name="username" value="root"></property>
        <property name="password" value="207863"></property>
        <!--驱动程序的类注入-->
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
    </bean>
    <!--jdbcTemplate模板对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

Test

TestDB

package annotation;

import annotation.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.SQLException;

public class TestDB {
    @Test
    public void TestAccount() throws SQLException {
        ApplicationContext context = new ClassPathXmlApplicationContext("annotation.xml");
        AccountService service = (AccountService) context.getBean("accountServiceImpl");
        service.transfer("A", "B", 100);
    }
}

运行结果

结果:可以看到,数据并未发生改变。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值