欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AOP之事務(wù)管理<aop:advisor>的兩種配置方式

 更新時(shí)間:2021年11月24日 08:51:04   作者:Simon_liu94  
這篇文章主要介紹了AOP之事務(wù)管理<aop:advisor>的兩種配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

AOP事務(wù)管理<aop:advisor>兩種配置方式

方式一

@transactionManagerbean.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.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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
     <context:component-scan base-package="com.wanho.java150"/>
     <context:property-placeholder location="classpath*:jdbc.properties"/>
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
         <property name="driverClassName" value="${jdbc.driverClassName}"/>
         <property name="url" value="${jdbc.url}"/>
         <property name="username" value="${jdbc.username}"/>
         <property name="password" value="${jdbc.password}"/>
    </bean>
     <!--spring 提供 jdbc 幫助類-->
     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
         <property name="dataSource" ref="dataSource"/>
     </bean>
    
     <!--基于@Transactional-->
     <!--事務(wù)管理器-->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="dataSource"/>
     </bean>
     <tx:annotation-driven />    
</beans>

ServiceImpl

在service實(shí)現(xiàn)類的最外層或者具體方法上添加@Transactional注解

@Service
//@Transactional
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDAO customerDAO ;
    @Autowired
    private LoginLogDAO loginLogDAO ;
    @Transactional
    @Override
    public Customer login(String account, String pswd) {
        return null;
    }
}

方式二

bean.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.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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
     <context:component-scan base-package="com.wanho.java150"/>
     <context:property-placeholder location="classpath*:jdbc.properties"/>
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
         <property name="driverClassName" value="${jdbc.driverClassName}"/>
         <property name="url" value="${jdbc.url}"/>
         <property name="username" value="${jdbc.username}"/>
         <property name="password" value="${jdbc.password}"/>
    </bean>
     <!--spring 提供 jdbc 幫助類-->
     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
         <property name="dataSource" ref="dataSource"/>
     </bean>
    
     <!--jdbc 事務(wù)管理配置基于xml-->
      <!--事務(wù)管理器-->
     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="dataSource"/>
     </bean>
     <!--事務(wù)隔離級(jí)別-->
     <tx:advice id="txAdvice" transaction-manager="txManager">
         <tx:attributes>
             <!--還可以添加回滾、只讀等標(biāo)簽配置-->
             <tx:method name="*" />
         </tx:attributes>
     </tx:advice>
     <!--業(yè)務(wù)層 使用 事務(wù)切面-->
     <aop:config>
         <aop:pointcut id="servicePointcut" expression="execution(* com.wanho.java150.service.impl.CustomerServiceImpl.*(..))"/>
         <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/>
     </aop:config>
</beans>

hibernate事務(wù)配置Aop aop:advisor模式

<!-- 使用HibernateTransactionManager管理hibernate事務(wù) -->
    <bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
        
    <!-- 創(chuàng)建事務(wù)規(guī)則 -->
    <!-- 表示我們要控制事務(wù)的地方,如果方法名開頭是add、update和delete,那么使用REQUIRED事務(wù)傳播方式。那么其他的方法使用REQUIRED事務(wù)傳播方式,并且是只讀 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
           <tx:method name="add*" propagation="REQUIRED"
          rollback-for="Exception" />
          <tx:method name="delete*" propagation="REQUIRED"
          rollback-for="Exception" />
          <tx:method name="update*" propagation="REQUIRED"
          rollback-for="Exception" />
          <tx:method name="*" propagation="REQUIRED" read-only="true" />
       </tx:attributes>
    </tx:advice>
    <!-- 告知事務(wù)的切入點(diǎn) -->
    <aop:config>
    <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.tiema..service..*.*(..))" />
    </aop:config>

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java集合-HashMap

    Java集合-HashMap

    這篇文章主要介紹了Java集合HashMap,也叫散列表,是一種非常重要的數(shù)據(jù)結(jié)構(gòu),應(yīng)用場景及其豐富,許多緩存技術(shù)(比如memcached)的核心其實(shí)就是在內(nèi)存中維護(hù)一張大的哈希表,下面來看看文章的具體內(nèi)容吧,需要的小伙伴也可參考一下
    2022-01-01
  • 基于JVM 調(diào)優(yōu)的技巧總結(jié)分析

    基于JVM 調(diào)優(yōu)的技巧總結(jié)分析

    本篇文章是對(duì)JVM 調(diào)優(yōu)的技巧進(jìn)行了總結(jié)和分析。需要的朋友參考下
    2013-05-05
  • SpringBoot整合MongoDB的步驟詳解

    SpringBoot整合MongoDB的步驟詳解

    這篇文章主要介紹了SpringBoot整合MongoDB的步驟詳解,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下
    2021-04-04
  • Spring、SpringMVC和SpringBoot的區(qū)別及說明

    Spring、SpringMVC和SpringBoot的區(qū)別及說明

    這篇文章主要介紹了Spring、SpringMVC和SpringBoot的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2022-10-10
  • 淺談Java中ABA問題及避免

    淺談Java中ABA問題及避免

    這篇文章主要介紹了淺談Java中ABA問題及避免,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • SpringBoot集成Swagger2實(shí)現(xiàn)Restful(類型轉(zhuǎn)換錯(cuò)誤解決辦法)

    SpringBoot集成Swagger2實(shí)現(xiàn)Restful(類型轉(zhuǎn)換錯(cuò)誤解決辦法)

    這篇文章主要介紹了SpringBoot集成Swagger2實(shí)現(xiàn)Restful(類型轉(zhuǎn)換錯(cuò)誤解決辦法),需要的朋友可以參考下
    2017-07-07
  • MyBatis 多表操作的實(shí)現(xiàn)

    MyBatis 多表操作的實(shí)現(xiàn)

    這篇文章主要介紹了MyBatis 多表操作的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Springboot項(xiàng)目對(duì)數(shù)據(jù)庫用戶名密碼實(shí)現(xiàn)加密過程解析

    Springboot項(xiàng)目對(duì)數(shù)據(jù)庫用戶名密碼實(shí)現(xiàn)加密過程解析

    這篇文章主要介紹了Springboot項(xiàng)目對(duì)數(shù)據(jù)庫用戶名密碼實(shí)現(xiàn)加密過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • java線性表排序示例分享

    java線性表排序示例分享

    這篇文章主要介紹了java線性表排序示例,需要的朋友可以參考下
    2014-03-03
  • SpringBoot事件機(jī)制相關(guān)知識(shí)點(diǎn)匯總

    SpringBoot事件機(jī)制相關(guān)知識(shí)點(diǎn)匯總

    這篇文章主要介紹了SpringBoot事件機(jī)制相關(guān)知識(shí)點(diǎn)匯總,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09

最新評(píng)論