spring复习:(42)配置文件的方式实现事务(TransactionProxyFactoryBean)

这篇具有很好参考价值的文章主要介绍了spring复习:(42)配置文件的方式实现事务(TransactionProxyFactoryBean)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、定义服务接口:

package cn.edu.tju.study.service.transaction2;

public interface StudentService {
    public void addStudent(String name, int age);
}

二、定义服务实现类

package cn.edu.tju.study.service.transaction2;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService{
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void addStudent(String name,int age) {
        String sql = "insert into student values('";
        sql += name;
        sql +="'";
        sql += ",";
        sql += age;
        sql += ")";
        jdbcTemplate.execute(sql);
        int i = 1/0;
        jdbcTemplate.execute(sql);

    }
}

三、配置文件:

<?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:tx="http://www.springframework.org/schema/tx"
       default-autowire="byName"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">


    <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://xxx.xxx.xxx.xxx/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="MyPa"></property>

    </bean>
    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>



    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="studentService" class="cn.edu.tju.study.service.transaction2.StudentServiceImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>


    <bean id="myFactoryBean"
          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="transactionManager" />
        <property name="target" ref="studentService" />
        <property name="transactionAttributes">
            <props>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>


</beans>

四、主类,调用TransactionProxyFactoryBean文章来源地址https://www.toymoban.com/news/detail-596269.html

package cn.edu.tju.study.service.transaction2;

import cn.edu.tju.study.service.MyConfig3;
import cn.edu.tju.study.service.aspect.MyDemoService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TransactionTest {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("transaction.xml");

        StudentService studentService = context.getBean("myFactoryBean", StudentService.class);

        studentService.addStudent("paul",23);
    }
}

到了这里,关于spring复习:(42)配置文件的方式实现事务(TransactionProxyFactoryBean)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 38、Spring事务的实现方式和原理以及隔离级别

    在使用Spring框架时,可以有两种使用事务的方式,一种是编程式的,一种是申明式的,@Transactional注解就是申明式的。 首先,事务这个概念是数据库层面的,Spring只是基于数据库中的事务进行了扩展,以及提供了一些能让程序员更加方便操作事务的方式。 比如我们可以通过在

    2024年02月16日
    浏览(32)
  • 【微服务】spring读取配置文件多种方式深入详解

    目录 一、前言 二、java配置文件介绍 2.1 java配置文件产生原因 2.2 项目使用配置文件好处 2.3 springboot项目配置文件的必要性 2.4 微服务架构下配置文件使用场景 三、java读取配置文件常用方法 3.1 使用Properties类读取配置文件 3.1.1 使用getResourceAsStream读取 3.1.2 使用getClassLoader读取

    2024年04月22日
    浏览(45)
  • Spring的加载配置文件、容器和获取bean的方式

    🐌个人主页: 🐌 叶落闲庭 💨我的专栏:💨 c语言 数据结构 javaweb 石可破也,而不可夺坚;丹可磨也,而不可夺赤。 properties文件: jdbc.properties 1.开启context命名空间 2.使用context命名空间,加载指定properties文件 3.使用属性占位符 ${} 读取properties文件中的属性 properties文件

    2024年02月15日
    浏览(27)
  • spring复习:(39)注解方式的ProxyFactoryBean

    一、定义接口 二、定义实现类: 三、定义配置类,配置业务bean、advisor bean、ProxyFactoryBean 四、定义主类,获取ProxyFactoryBean并使用 五、运行结果

    2024年02月16日
    浏览(23)
  • 【Spring】1、Spring 框架的基本使用【读取配置文件、IoC、依赖注入的几种方式、FactoryBean】

    Spring 框架可以说是 Java 开发中最重要的框架,功能 非常 强大 中文文档:https://springdoc.cn/spring/ 官网:https://spring.io/ Spring makes Java Simple、modern、productive … Spring 框架的几个核心概念: IoC: I nversion o f C ontrol:控制反转 DI: D ependency I njection:依赖注入 AOP: A spect O riented P rogram

    2024年02月09日
    浏览(31)
  • SSM项目集成Spring Security 4.X版本(使用spring-security.xml 配置文件方式)

    目录 前言 实战开发: 一、Spring Security整合到SSM项目 1. pom文件引入包 2. web.xml 配置 3. 添加 spring-security.xml 文件 二、Spring Security实战应用 1. 项目结构 2. pom文件引入 3. web.xml 配置 4. Spring 配置 applicationContext.xml 5. spring-security.xml 配置 6. springmvc.xml 配置 7. 创建实体类 8. DAO层实

    2024年01月24日
    浏览(37)
  • Spring Boot实现文件上传的两种方式

    最近的一个小项目里使用到了文件上传、下载功能,今天我打算梳理一下文件上传所涉及的技术及实现。 内容主要包括两部分,如何通过纯 Servlet 的形式进行文件上传、保存(不通过 Spring 框架);另一部分是如何在 Spring Web MVC 中进行文件上传。 HTTP 协议传输文件一般都遵循

    2024年02月05日
    浏览(33)
  • Spring Boot中操作数据库的几种并发事务方式

    当有多个 并发 事务时,会发生丢失更新异常。来自一个或多个 事务 的更新可能会丢失,因为其他事务会用其结果覆盖它。 让我们通过一个例子来检验一下。考虑以下执行事务的方法。 public void withdraw(Long accountId, double amount) { Account account = accountRepository.findById(accountId).orEl

    2024年01月22日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包