1. 程式人生 > >eclipse下maven管理Spring專案構:SpringAOP,基於XMl,基於註解宣告事務,及事務的傳播行為

eclipse下maven管理Spring專案構:SpringAOP,基於XMl,基於註解宣告事務,及事務的傳播行為

一:在applicationContext.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:aop="http://www.springframework.org/schema/aop"     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-4.3.xsd         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

 //包掃描         <context:component-scan base-package="com.qst"/>

//開啟註解         <aop:aspectj-autoproxy proxy-target-class="true"/>  //資料來源。有三種資料庫連線池的配置。 這個是用JDBC連線的Mysql         <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">             <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>             <property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf8"></property>             <property name="username" value="root"></property>             <property name="password" value="root"></property>         </bean>         <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">             <property name="dataSource" ref="dataSource"></property>         </bean>

//以上不管是基於XML還是基於註解都需要

//基於註解需要加入此句         <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">             <property name="dataSource" ref="dataSource"></property>         </bean>         <tx:annotation-driven transaction-manager="txManager"/>  //基於XML         <!-- 事務通知 -->         <!-- <tx:advice id="myAdvice" transaction-manager="txManager">             <tx:attributes>                 <tx:method name="add" propagation="REQUIRED" isolation="DEFAULT" read-only="false" rollback-for="RuntimeException"/>                 <tx:method name="addlog" propagation="REQUIRES_NEW"/>//propagation事物的傳播行為                 <tx:method name="delete*"/>                 <tx:method name="update*"/>                 <tx:method name="select*" read-only="true"/>             </tx:attributes>         </tx:advice>         <aop:config>             <aop:pointcut expression="execution(* com.qst.service.*.*(..))" id="mytxpointcut"/>             <aop:advisor advice-ref="myAdvice" pointcut-ref="mytxpointcut"/>         </aop:config> -->                  <!-- <bean id="business" class="com.qst.BusinessTest"></bean>                  <bean id="aspect" class="com.qst.AspectAop"></bean>          //SpringAOP        

<aop:config>             <aop:aspect ref="aspect">                 <aop:pointcut expression="execution(* com.qst.*.*(..))" id="mypointcut"/>                 <aop:before method="before" pointcut-ref="mypointcut"/>                 <aop:after method="after" pointcut-ref="mypointcut"/>                 <aop:after-returning method="afterReturning" pointcut-ref="mypointcut"/>                 <aop:after-throwing method="except" pointcut-ref="mypointcut" throwing="e"/>                 <aop:around method="around" pointcut-ref="mypointcut"/>             </aop:aspect>         </aop:config> -->              <!-- <bean id="helloTest" class="com.qst.HelloTest" scope="prototype" init-method="init" destroy-method="destroy"/>          <bean id="staticFactory" class="com.qst.SingleStaticFactoryBean" factory-method="getSingleTest"/>          <bean id="factory" class="com.qst.FactoryBean"></bean>     <bean id="test" factory-bean="factory" factory-method="getSingTest"></bean>          <bean id="userDao" class="com.qst.dao.UserDaoImpl"></bean>          <bean id="userService" class="com.qst.service.UserServiceImpl">         <constructor-arg index="0" ref="userDao"></constructor-arg>     </bean>          <bean id="userService" class="com.qst.service.UserServiceImpl">         <property name="userDao" ref="userDao"></property>     </bean> -->     <!-- <bean id="userController" class="com.qst.controller.UserController">         <property name="list">             <list>                 <value></value>                 <bean></bean>             </list>         </property>         <map></map>         <set></set>     </bean> --> </beans>

事務傳播行為型別

說明

PROPAGATION_REQUIRED

如果當前沒有事務,就新建一個事務,如果已經存在一個事務中,加入到這個事務中。這是最常見的選擇。

PROPAGATION_SUPPORTS

支援當前事務,如果當前沒有事務,就以非事務方式執行。

PROPAGATION_MANDATORY

使用當前的事務,如果當前沒有事務,就丟擲異常。

PROPAGATION_REQUIRES_NEW

新建事務,如果當前存在事務,把當前事務掛起。

PROPAGATION_NOT_SUPPORTED

以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。

PROPAGATION_NEVER

以非事務方式執行,如果當前存在事務,則丟擲異常。

PROPAGATION_NESTED

如果當前存在事務,則在巢狀事務內執行。如果當前沒有事務,則執行與PROPAGATION_REQUIRED類似的操作。