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.pswp.cn/news/391336.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391336.shtml
英文地址,請注明出處:http://en.pswp.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…