Spring-----AOP-----事务

   xml文件中:

 

手动处理事务:


设置数据源

<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/transfer"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>

      创建事务管理器,因为使用的是jdbc操作数据库,所以使用DataSourceTransactionManager事务管理需要事务,事务来自Connection,即来自连接池,所以要

     注入数据源。

   

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


创建事务模板,事务模板需要事务,事务在管理器中,所以需要注入事务管理器
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="txManager"></property>
</bean>

接下来就是具体的实现类来注入事务模板

<!--创建service对象,并注入dao,注入事务-->
<bean id="accountServiceId" class="com.luo.transfer2.dao.serviceImpl.AccountServiceImpl">
<property name="accountDao" ref="accountDaoId"></property>
<property name="transactionTemplate" ref="transactionTemplate"></property>
</bean>


Spring半自动管理事务(使用SpringFactoryBean创建代理对象)

使用Spring的半自动方式生成代理对象,Service就是目标类,事务就是切面类
<bean id="proxyBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
代理对象要实现的接口
    <property name="proxyInterfaces" value="com.luo.transfer3.service.AccountService"></property>

目标类
   <property name="target" ref="accountServiceId"></property>

切面类
<property name="transactionManager" ref="txManager"></property>

//注入事务的一些属性
  <property name="transactionAttributes">
<!--key是service中的方法名,
PROPAGATION ISOLATION -EXCEPTION +EXCEPTION
发生异常时回滚 发生异常时也不回滚
-->
<props>
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
</props>
</property>

-------------------------------------------------------------------------------------------------

全自动方式 使用Spring的aop创建代理对象
                            
<tx:advice transaction-manager="txManager" id="aspectId">
<!--配置事务的属性-->
<tx:attributes>
<tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:advisor advice-ref="aspectId" pointcut="execution(* com.luo.transfer4.serviceImpl..*.*(..))"></aop:advisor>
</aop:config>


<!--创建事务管理器,因为使用的是jdbc操作数据库,所以使用DataSourceTransactionManager
事务管理需要事务,事务来自Connection,即来自连接池,所以要注入数据源
-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>























             

转载于:https://www.cnblogs.com/Leroyo/p/8360270.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391336.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[测试题]打地鼠

Description 小明听说打地鼠是一件很好玩的游戏&#xff0c;于是他也开始打地鼠。地鼠只有一只&#xff0c;而且一共有N个洞&#xff0c;编号为1到N排成一排&#xff0c;两边是墙壁&#xff0c;小明当然不可能百分百打到&#xff0c;因为他不知道地鼠在哪个洞。小明只能在白天打…

在PHP服务器上使用JavaScript进行缓慢的Loris攻击[及其预防措施!]

Forget the post for a minute, lets begin with what this title is about! This is a web security-based article which will get into the basics about how HTTP works. Well also look at a simple attack which exploits the way the HTTP protocol works.暂时忘掉这个帖…

三星为什么要卖芯片?手机干不过华为小米,半导体好挣钱!

据外媒DigiTimes报道&#xff0c;三星有意向其他手机厂商出售自家的Exynos芯片以扩大市场份额。知情人士透露&#xff0c;三星出售自家芯片旨在提高硅晶圆工厂的利用率&#xff0c;同时提高它们在全球手机处理器市场的份额&#xff0c;尤其是中端市场。 三星为什么要卖芯片&…

重学TCP协议(2) TCP 报文首部

1. TCP 报文首部 1.1 源端口和目标端口 每个TCP段都包含源端和目的端的端口号&#xff0c;用于寻找发端和收端应用进程。这两个值加上IP首部中的源端IP地址和目的端IP地址唯一确定一个TCP连接 端口号分类 熟知端口号&#xff08;well-known port&#xff09;已登记的端口&am…

linux:vim中全选复制

全选&#xff08;高亮显示&#xff09;&#xff1a;按esc后&#xff0c;然后ggvG或者ggVG 全部复制&#xff1a;按esc后&#xff0c;然后ggyG 全部删除&#xff1a;按esc后&#xff0c;然后dG 解析&#xff1a; gg&#xff1a;是让光标移到首行&#xff0c;在vim才有效&#xf…

机器学习 预测模型_使用机器学习模型预测心力衰竭的生存时间-第一部分

机器学习 预测模型数据科学 &#xff0c; 机器学习 (Data Science, Machine Learning) 前言 (Preface) Cardiovascular diseases are diseases of the heart and blood vessels and they typically include heart attacks, strokes, and heart failures [1]. According to the …

程序2:word count

本程序改变自&#xff1a;http://blog.csdn.net/zhixi1050/article/details/72718638 语言&#xff1a;C 编译环境&#xff1a;visual studio 2015 运行环境&#xff1a;Win10 做出修改的地方&#xff1a;在原码基础上修改了记录行数的功能&#xff0c;删去了不完整行数的记录&…

重学TCP协议(3) 端口号及MTU、MSS

1. 端口相关的命令 1.1 查看端口是否打开 使用 nc 和 telnet 这两个命令可以非常方便的查看到对方端口是否打开或者网络是否可达。如果对端端口没有打开&#xff0c;使用 telnet 和 nc 命令会出现 “Connection refused” 错误 1.2 查看监听端口的进程 使用 netstat sudo …

Diffie Hellman密钥交换

In short, the Diffie Hellman is a widely used technique for securely sending a symmetric encryption key to another party. Before proceeding, let’s discuss why we’d want to use something like the Diffie Hellman in the first place. When transmitting data o…

高效能程序猿的修炼

下载地址&#xff1a;http://download.csdn.net/detail/xiaole0313/8931785 高效能程序猿的修炼 《高效能程序猿的修炼是人民邮电出版社出版的图书。本书是coding horror博客中精华文章的集合。全书分为12章。涉及迈入职业门槛、高效能编程、应聘和招聘、团队协作、高效工作环境…

Spring 中的 LocalSessionFactoryBean和LocalContainerEntityManagerFactoryBean

Spring和Hibernate整合的时候我们经常会有如下的配置代码 1&#xff0c;非JPA支持的配置 <!-- 配置 Hibernate 的 SessionFactory 实例: 通过 Spring 提供的 LocalSessionFactoryBean 进行配置 --> <!-- FacotryBean 配置的时候返回的不是本身而是返回的FactoryBean 的…

如何通过建造餐厅来了解Scala差异

I understand that type variance is not fundamental to writing Scala code. Its been more or less a year since Ive been using Scala for my day-to-day job, and honestly, Ive never had to worry much about it. 我了解类型差异并不是编写Scala代码的基础。 自从我在日…

linux的/etc/passwd、/etc/shadow、/etc/group和/etc/gshadow

1./etc/passwd 存储用户信息 [rootoldboy ~]# head /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin 一行记录对应着一个用户&#xff0c;每行记录被冒号:分隔为7个字段&#xff0c;这7个字段的具体含…

组织在召唤:如何免费获取一个js.org的二级域名

之前我是使用wangduanduan.github.io作为我的博客地址&#xff0c;后来觉得麻烦&#xff0c;有把博客关了。最近有想去折腾折腾。先看效果&#xff1a;wdd.js.org 如果你不了解js.org可以看看我的这篇文章:一个值得所有前端开发者关注的网站js.org 前提 已经有了github pages的…

linkedin爬虫_您应该在LinkedIn上关注的8个人

linkedin爬虫Finding great mentors are hard to come by these days. With so much information and so many opinions flooding the internet, finding an authority in a specific field can be quite tough.这些天很难找到优秀的导师。 互联网上充斥着如此众多的信息和众多…

重学TCP协议(4) 三次握手

1. 三次握手 请求端&#xff08;通常称为客户&#xff09;发送一个 S Y N段指明客户打算连接的服务器的端口&#xff0c;以及初始序号。这个S Y N段为报文段1。服务器发回包含服务器的初始序号的 S Y N报文段&#xff08;报文段2&#xff09;作为应答。同时&#xff0c;将确认序…

[设计模式]State模式

《Java与模式》 又称状态对象模式。状态模式是对象的行为模式。GOF95 一个对象的行为取决于一个或者多个动态变化的属性&#xff0c;这样的属性叫做状态。这样的对象叫做有状态的对象&#xff08;stateful&#xff09;。 状态模式把一个所研究的对象的行为包装在不同的状态对象…

java温故笔记(二)java的数组HashMap、ConcurrentHashMap、ArrayList、LinkedList

为什么80%的码农都做不了架构师&#xff1f;>>> HashMap 摘要 HashMap是Java程序员使用频率最高的用于映射(键值对)处理的数据类型。随着JDK&#xff08;Java Developmet Kit&#xff09;版本的更新&#xff0c;JDK1.8对HashMap底层的实现进行了优化&#xff0c;例…

前置交换机数据交换_我们的数据科学交换所

前置交换机数据交换The DNC Data Science team builds and manages dozens of models that support a broad range of campaign activities. Campaigns rely on these model scores to optimize contactability, volunteer recruitment, get-out-the-vote, and many other piec…

aws 弹性三剑客_AWS和弹性:超越用户需求

aws 弹性三剑客I’ll assume that, one way or another, you’re already familiar with many of AWS’s core deployment services. That means you now know about:我假设您已经熟悉许多AWS的核心部署服务。 这意味着您现在知道&#xff1a; • EC2 instances and AMIs (Ama…