Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法

本文主要是介绍Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- c3p0连接池得到dataSource --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sw_database"></property><property name="user" value="root"></property><property name="password" value="root"></property></bean><!-- 配置事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 开启事务注解 --><tx:annotation-driven transaction-manager="transactionManager"/><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><bean id="accountDao" class="com.swift.AccountDao"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean><bean id="accountService" class="com.swift.AccountService"><property name="accountDao" ref="accountDao"></property></bean></beans>

事务的注解方法依然需要事务管理器DataSourceTransactionManager,这个管理器当然需要数据源DataSource来确认指向哪个数据库,即注入dataSource。

然后开启事务注解的扫描<tx:annotation-driven transaction-manager="transactionManager"/>

程序中在需要增强的类上用@Transactional标记就可以了,自动调用对该类所有方法进行增强的事务,不用再像前边的配置文件方法,自己定义

省去了下面步骤

<!--  配置事务增强 --><tx:advice id="txadvice" transaction-manager="transactionManager"><!-- 做事务操作 --><tx:attributes><!-- 事务操作的方法匹配规则 --><tx:method name="money*" propagation="REQUIRED"/></tx:attributes></tx:advice>

也省去了切入点、切面的aop操作

<!-- 配置切面 --><aop:config><!-- 切入点 --><aop:pointcut expression="execution(* com.swift.AccountService.*(..))" id="pointcut1"/><!-- 切面 --><aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/></aop:config>

 

其他的类dao类和Service类没有改变

代码如下:

package com.swift;import org.springframework.jdbc.core.JdbcTemplate;public class AccountDao {private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}//少钱的方法public void lessMoney(String from,double number) {String sql="update account set money=money-? where username=?";jdbcTemplate.update(sql, number,from);}//多钱的方法public void moreMoney(String to,double number) {String sql="update account set money=money+? where username=?";jdbcTemplate.update(sql, number,to);}
}

Service类

package com.swift;import org.springframework.transaction.annotation.Transactional;@Transactional 
public class AccountService {private AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}public void moneyTransfer(String from,String to,double number) {accountDao.lessMoney(from,number);int i=10/0;accountDao.moreMoney(to,number);}
}

配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法

下边是注解方法注入属性

package com.swift;import javax.annotation.Resource;public class Service {@Resource(name="studentDao")private StudentDao studentDao;@Resource(name="courseDao")private CourseDao courseDao;public String fun() {return "This is Service's fun()........."+this.studentDao.fun()+this.courseDao.fun();}
}

aop操作复习

    <bean id="book" class="com.swift.aop.Book"></bean><bean id="adviceBook" class="com.swift.aop.AdviceBook"></bean><aop:config><!-- 切入点表达式第一个*表示public private等权限后接空格,第二个*表示任意方法(..)表示参数 --><aop:pointcut expression="execution(* com.swift.aop.Book.*())" id="pointcut1"/><!-- 哪个切面(用来增强切入点的类) 名称是什么用ref表示 --><aop:aspect ref="adviceBook"><!-- 增强的具体方法,增强哪个切入点 --><aop:before method="before" pointcut-ref="pointcut1"/></aop:aspect></aop:config>

只要调用book对象中的任意方法就可以得到增强后的效果

 

这篇关于Spring中使用事务搭建转账环境方法二 相对简便的注解方法 ——配置文件注入对象属性需要setter方法 注解方法,不需要生成setter方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/762194

相关文章

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load