欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

如何在Spring中使用聲明式事務(wù)管理-創(chuàng)新互聯(lián)

本篇文章為大家展示了如何在Spring中使用聲明式事務(wù)管理,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

目前創(chuàng)新互聯(lián)公司已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計、寧化網(wǎng)站維護等服務(wù),公司將堅持客戶導向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

基于TransactionProxyFactoryBean的事務(wù)管理配置

Spring中配置AOP有三種方式,分別是通過ProxyFactoryBean創(chuàng)建代理,通過XML的方式以及通過注解的方式,既然Spring事務(wù)管理是通過AOP來實現(xiàn)的,那么對應(yīng)的就有三種不同的方式,首先來看下基于TransactionProxyFactoryBean的管理方式

首先是Spring的配置文件

<?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"
    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">

  <!--開啟自動掃描-->
  <context:component-scan base-package="cn.xuhuanfeng.transaction"/>

  <!--配置數(shù)據(jù)源,這里采用dbcp-->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="url" value="jdbc:mysql://localhost:3306/spring"/>
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="username" value="root"/>
    <property name="password" value="huanfeng"/>
  </bean>

  <!--配置JdbcTemplate-->
  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <!--注入數(shù)據(jù)源-->
    <property name="dataSource" ref="dataSource"/>
  </bean>

  <!--配置事務(wù)管理-->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!--注入數(shù)據(jù)源-->
    <property name="dataSource" ref="dataSource"/>
  </bean>
  <!--為AccountService創(chuàng)建代理類-->
  <bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <!--注入事務(wù)管理-->
    <property name="transactionManager" ref="transactionManager"/>
    <!--注入目標類,也就是所要增強的類-->
    <property name="target" ref="accountService"/>
    <!--配置相應(yīng)的事務(wù)屬性-->
    <property name="transactionAttributes">
      <props>
        <!--指定不同的事務(wù)的處理方式
          配置格式:事務(wù)傳播方式,隔離級別,readOnly,-Exception,+Exception
          傳播行為是唯一必須配置的,其他的如果不配置則使用默認
          -Exception表示如果發(fā)生對應(yīng)的異常,則回滾事務(wù)
          +Exception表示即使發(fā)生對應(yīng)的異常,也依舊提交事務(wù)
        -->
        <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
      </props>
    </property>
  </bean>
</beans>

對應(yīng)的持久層代碼

@Repository
public class AccountDao {

  @Autowired
  private JdbcTemplate jdbcTemplate;

  public void transferIn(String name, double money){
    String sql = "update account set money = money + ? where name = ?";

    jdbcTemplate.update(sql, money, name);
  }

  public void transferOut(String name, double money){
    String sql = "update account set money = money - ? where name = ?";

    jdbcTemplate.update(sql, money, name);
  }
}

業(yè)務(wù)層代碼

@Service
public class AccountService {

@Autowired
private AccountDao accountDao;

public void transfer(final String fromName,final String toName,final double money){

  accountDao.transferOut(fromName, money);
  int d = 1/0; // 除0異常
  accountDao.transferIn(toName, money);
}
}

通過上面的配置之后,當我們在使用AccountService的時候,由于獲取的對象的代理后的對象,所以Spring會自動進行事務(wù)的監(jiān)管,而我們需要做的就是配置對應(yīng)的事務(wù)傳播類型以及事務(wù)管理級別等的信息,這種方式明顯對代碼以及沒有什么侵入了,但是使用這種方式意味著沒有都需要為不同的服務(wù)對象創(chuàng)建對應(yīng)的代理對象,這其實是不太方便的,接下來我們來看下使用aop/tx命名空間來進行配置的方式。

基于aop/tx命名空間的事務(wù)管理配置

由于是對上面的業(yè)務(wù)操作進行事務(wù)管理,而且經(jīng)過上一小節(jié)的學習,我們也基本熟悉了該業(yè)務(wù),所以這里直接演示配置的代碼

<?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.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 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd">

  <!--
    這里配置同前,故省略
  -->

  <!--aop配置-->
  <aop:config>
    <!--配置切點-->
    <aop:pointcut id="serviceMethod" expression="execution(* cn.xuhuanfeng.transaction.AccountService.*(..))"/>
    <!--對應(yīng)的切面-->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
  </aop:config>

  <!--配置事務(wù)增強-->
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <!--配置對應(yīng)的事務(wù)管理,其中name為與事務(wù)相關(guān)的方法名,可以使用通配符-->
      <tx:method name="transfer*" isolation="DEFAULT" propagation="REQUIRED"/>
    </tx:attributes>
  </tx:advice>

</beans>

可以看到,通過XML配置的方式,可以更加靈活地進行事務(wù)管理

基于注解的事務(wù)管理配置

基于注解的配置方式提供了更加簡單的配置方式,只需要使用@Transactional注解進行標注,并且開啟對應(yīng)的掃描即可。

// 配置相應(yīng)的隔離級別、事務(wù)傳播等
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
@Service
public class AccountService {
  // 省略其他內(nèi)容
}

Spring配置文件也相對比較簡單了

<?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.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">

  <!--數(shù)據(jù)源配置等同上-->
  <!--通過tx命名空間,開啟主機自動掃描,并且注入事務(wù)管理器-->
  <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

可以看到,通過注解配置的方式是最簡單的配置方式,在日常的開發(fā)中,這種方式的使用的頻率也比較高

上述內(nèi)容就是如何在Spring中使用聲明式事務(wù)管理,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁標題:如何在Spring中使用聲明式事務(wù)管理-創(chuàng)新互聯(lián)
URL地址:http://aaarwkj.com/article26/dddhcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、品牌網(wǎng)站建設(shè)、動態(tài)網(wǎng)站、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)
人妻中出中文字幕一区| 午夜日本大胆裸体艺术| 亚洲毛片在线免费播放| 中文字幕一区二区三区三级| 国产美女冒白浆免费网站| 国产欧美精品久久三级| 97在线视频观看视频在线| 亚洲熟女内射特写一区| 亚洲国产自拍精品视频| 人妻口爆视频一区二区三区| 97在线视频观看视频在线| 亚洲精品日韩在线欧美| 欧美日韩另类中文字幕| 一本色桃子精品久久中文字幕| 日本免费91午夜视频| 亚洲精品有码在线观看| 日韩黄av在线免费观看| 亚洲人成伊人久久成| 亚洲精品欧美日韩久久| 国产一区二区三区本色| 懂色av中文一区二区| 亚洲天堂福利视频网站| 欧美激情在线精品一区二区| 日本丝袜福利在线观看| 国产精彩在线视频成人在线| 91无人区一区二区三乱码| 内射性感黑丝少妇av| 亚洲一区二区三区久久伊人| 国产精品国产三级区别| 2020年最新国产三级网站| 一本久道久久综合久久鬼色| 日韩精品一区高清视频| 人人妻人人澡人人爽人人老司机| 精品人妻一区二区三区不卡| 久久久亚洲福利精品午夜| 在线播放精品免费不卡| 精品视频美女肉体亚洲| 中文字幕色视频在线观看| 成人福利在线观看免费视频| 东京热男人的av天堂| 国产亚洲欧美日韩中文字幕|