課程
/后端開發
/Java
/Spring事務管理
我們公司使用的是攔截的方式。但是沒見老師講這一種。心里好沒底兒啊!這種攔截的方式是否也是常用的。,
2019-04-20
源自:Spring事務管理 7-1
正在回答
老師講過了。。。。。。。。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
? ? ? ?xmlns:tx="http://www.springframework.org/schema/tx"
? ? ? ?xmlns:aop="http://www.springframework.org/schema/aop"
? ? ? ?xmlns:context="http://www.springframework.org/schema/context"
? ? ? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ?xsi:schemaLocation="
? ? ? ?http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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
? ? ? ? http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
? ? <!--配置數據源c3p0-->
? ? <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
? ? ? ? <property name="driverClass" value="com.mysql.jdbc.Driver"/>
? ? ? ? <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"/>
? ? ? ? <property name="user" value="root"/>
? ? ? ? <property name="password" value="root"/>
? ? </bean>
? ? <!--配置業務層實現類-->
? ? <bean class="spring.demo2.AccountServiceImpl" id="accountService">
? ? ? ? <property name="accountDao" ref="accountDao"/>
? ? <!--配置DAO類-->
? ? <bean class="spring.demo2.AccountDaoImpl" id="accountDao">
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? <!--配置事務管理器-->
? ? <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
? ? <!--配置業務層的代理-->
? ? <bean class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
? ? ? ? ? id="transactionProxyFactoryBean">
? ? ? ? <!--目標對象-->
? ? ? ? <property name="target" ref="accountService"/>
? ? ? ? <!--注入事務管理器-->
? ? ? ? <property name="transactionManager" ref="transactionManager"/>
? ? ? ? <!--注入屬性-->
? ? ? ? <property name="transactionAttributes">
? ? ? ? ? ? <props>
? ? ? ? ? ? ? ? <!--
? ? ? ? ? ? ? ? prop的格式:
? ? ? ? ? ? ? ? PROPAGATION:事務傳播行為
? ? ? ? ? ? ? ? ISOLATIN:事務的隔離行為
? ? ? ? ? ? ? ? readOnly:只讀
? ? ? ? ? ? ? ? -Exception:發生哪些異常回滾事務
? ? ? ? ? ? ? ? +Exception:發生哪些異常事務不回滾
? ? ? ? ? ? ? ? -->
? ? ? ? ? ? ? ? <!--*代表類中的所有方法-->
? ? ? ? ? ? ? ? <prop key="*">PROPAGATION_REQUIRED</prop>
? ? ? ? ? ? </props>
? ? ? ? </property>
</beans>
舉報
事務管理是Spring重要的知識,應用事務解決數據不一致問題
1 回答還有一種基于攔截器的方式吧?
1 回答代理的方式
2 回答求老師的xml快捷鍵方式
1 回答注解方式能注解在private修飾的方法上嗎
1 回答Spring事務中,基于AspectJ的XML方式和基于注解的方式可以同時使用嗎?如果可以同時使用,哪個優先級高?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2019-06-18
基于TransactionProxyFactoryBean的方式(實際開發種不經常使用)
老師講過了。。。。。。。。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
? ? ? ?xmlns:tx="http://www.springframework.org/schema/tx"
? ? ? ?xmlns:aop="http://www.springframework.org/schema/aop"
? ? ? ?xmlns:context="http://www.springframework.org/schema/context"
? ? ? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ?xsi:schemaLocation="
? ? ? ?http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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
? ? ? ? http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
? ? <!--配置數據源c3p0-->
? ? <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
? ? ? ? <property name="driverClass" value="com.mysql.jdbc.Driver"/>
? ? ? ? <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"/>
? ? ? ? <property name="user" value="root"/>
? ? ? ? <property name="password" value="root"/>
? ? </bean>
? ? <!--配置業務層實現類-->
? ? <bean class="spring.demo2.AccountServiceImpl" id="accountService">
? ? ? ? <property name="accountDao" ref="accountDao"/>
? ? </bean>
? ? <!--配置DAO類-->
? ? <bean class="spring.demo2.AccountDaoImpl" id="accountDao">
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? </bean>
? ? <!--配置事務管理器-->
? ? <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? </bean>
? ? <!--配置業務層的代理-->
? ? <bean class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
? ? ? ? ? id="transactionProxyFactoryBean">
? ? ? ? <!--目標對象-->
? ? ? ? <property name="target" ref="accountService"/>
? ? ? ? <!--注入事務管理器-->
? ? ? ? <property name="transactionManager" ref="transactionManager"/>
? ? ? ? <!--注入屬性-->
? ? ? ? <property name="transactionAttributes">
? ? ? ? ? ? <props>
? ? ? ? ? ? ? ? <!--
? ? ? ? ? ? ? ? prop的格式:
? ? ? ? ? ? ? ? PROPAGATION:事務傳播行為
? ? ? ? ? ? ? ? ISOLATIN:事務的隔離行為
? ? ? ? ? ? ? ? readOnly:只讀
? ? ? ? ? ? ? ? -Exception:發生哪些異常回滾事務
? ? ? ? ? ? ? ? +Exception:發生哪些異常事務不回滾
? ? ? ? ? ? ? ? -->
? ? ? ? ? ? ? ? <!--*代表類中的所有方法-->
? ? ? ? ? ? ? ? <prop key="*">PROPAGATION_REQUIRED</prop>
? ? ? ? ? ? </props>
? ? ? ? </property>
? ? </bean>
</beans>