Spring中基於xml和註解的聲明式事務配置

基於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: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/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">

    <!--配置賬戶的持久層-->
    <bean id="accountDao" class="com.sx.dao.impl.AccountDaoImpl">
        <!--<property name="jdbcTemplate" ref="jdbcTemplate"></property>-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置數據源-->
    <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/eesy_spring"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>

    <!--配置賬戶業務層-->
    <bean id="accountService" class="com.sx.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <!--Spring中基於xml的聲明式事務控制
        1.配置事務管理器
        2.配置事務通知
            <tx:advice>標籤配置事務通知
                id屬性給事務通知提供一個唯一標誌
                transaction-manager屬性給事務通知提供一個事務管理器引用
        3.配置AOP中的通用切入點表達式
        4.建立事務通知和切入點表達式的對應關係
        5.配置事務屬性
            isolation屬性:用於指定事務隔離級別,默認值DEFAULT,表示使用數據庫的默認級別
            no-rollback-for屬性:於指定一個異常,當產生該異常時事務不回滾,產生其他異常時,事務回滾。沒有默認值。表示任何異常都回滾
            propagation屬性:用於指定書屋的傳播行爲,默認值REQUIRED,查詢可選SUPPORTS
            read-only屬性:設置事務是否只讀,只用查詢方法才能設置爲true,默認值時false便是讀寫
            rollback-for屬性:用於指定一個異常,當產生該異常時事務回滾,產生其他異常時,事務不回滾。沒有默認值。表示任何異常都回滾
            timeout屬性:用於指定事務的超時時間,默認值-1,如果制定了時間以s爲單位
    -->
    <!--配置事務管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置事務通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--配置事務屬性-->
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" read-only="false"/>
            <!--優先級高-->
            <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!--配置AOP-->
    <aop:config>
        <!--配置切入點表達式-->
        <aop:pointcut id="pt1" expression="execution(* com.sx.service.impl.*.*(..))"></aop:pointcut>
        <!--建立事務通知和切入點表達式的對應關係-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
    </aop:config>
</beans>

基於註解(非純註解)

<?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:tx="http://www.springframework.org/schema/tx"
       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/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">

    <context:component-scan base-package="com.sx"></context:component-scan>

    <!--配置數據源-->
    <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/eesy_spring"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--Spring中基於註解的聲明式事務控制
        1.配置事務管理器
        2.開啓Spring對註解事務的支持
        3.在需要事務支持的地方使用@Transactional註解
    -->
    <!--配置事務管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--開啓Spring對註解事務的支持-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

業務層實現類

@Service("accountService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private IAccountDao accountDao;

    @Override
    public Account findAccountById(Integer accountId) {
        return accountDao.findAccountById(accountId);

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void transfer(String sourceName, String targetName, Float money) {
        System.out.println("transfer....");
        //2.1根據名稱查詢轉出賬戶
        Account source = accountDao.findAccountByName(sourceName);
        //2.2根據名稱查詢轉入賬戶
        Account target = accountDao.findAccountByName(targetName);
        //2.3轉出賬戶減錢
        source.setMoney(source.getMoney() - money);
        //2.4轉入賬戶加錢
        target.setMoney(target.getMoney() + money);
        //2.5更新轉出賬戶
        accountDao.updateAccount(source);
        int i = 1 / 0;
        //2.6更新轉入賬戶
        accountDao.updateAccount(target);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章