天天看點

spring aop 切面程式設計控制管理事務

1   一個業務邏輯方法中需要有操作插入兩張表記錄互相關聯依賴資料,

2   要保證兩張表要麼都插入成功要麼都插入失敗,

3   否則隻成功插入任何一張表都是毫無意義的垃圾資料

本人想到spring AOP 事務管理能支援此業務需求功能,

具體配置如下經驗證:

1 DAO接口定義顯示抛出異常

2 産生異常處catch時候 { throw new  runtimeException} 這個是spring預設復原的異常

<!--aop-->配置

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

    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:security="http://www.springframework.org/schema/security"

    xsi:schemaLocation="

         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

           http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd

          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- transaction -->

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean name="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="create*" propagation="REQUIRED"

                rollback-for="java.lang.Exception" />

            <tx:method name="remove*" propagation="REQUIRED"

                rollback-for="java.lang.Exception" />

            <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />

            <tx:method name="save*" propagation="REQUIRED"

                rollback-for="java.lang.Exception" />

            <tx:method name="delete*" propagation="REQUIRED"

                rollback-for="java.lang.Exception" />

            <tx:method name="update*" propagation="REQUIRED"

                rollback-for="java.lang.Exception" />

            <tx:method name="insert*" propagation="REQUIRED"

                rollback-for="java.lang.Exception" />

            <tx:method name="log*" propagation="REQUIRED" rollback-for="java.lang.Exception" />

            <tx:method name="next*" propagation="REQUIRED"

                rollback-for="java.lang.Exception" />

            <tx:method name="*" read-only="true" />

            <tx:method name="do*" propagation="REQUIRED"

                rollback-for="java.lang.Exception" />

            <tx:method name="*" read-only="true" />

        </tx:attributes>

    </tx:advice>

    <aop:config>

        <aop:advisor pointcut="execution(* com.saic.grape.dao.impl..*.*(..))"

            advice-ref="txAdvice" order="1" />

        <aop:advisor pointcut="execution(* com.saic.grape.dop.dao.impl..*.*(..))"

            advice-ref="txAdvice" order="2" />

    </aop:config>

</beans>

<!--DAO操作-->

package com.saic.grape.dao;

import java.util.List;

import java.util.Map;

import com.saic.grape.entity.City;

public interface CityDAO {

    public int createTable(Map map) throws  Exception;

}

package com.saic.grape.dao.impl;

import java.util.List;

import java.util.Map;

import org.springframework.stereotype.Repository;

import com.saic.grape.base.dao.CommonBaseDao;

import com.saic.grape.dao.CityDAO;

import com.saic.grape.entity.City;

@Repository("cityDAO")

public class CityDAOImpl extends CommonBaseDao<City, String> implements CityDAO {

    private static final String nameSpace = "com.saic.grape.dao.CityDAO.";

    @Override

    public int createTable(Map map) throws Exception {

        // TODO Auto-generated method stub

        int re1= super.getSqlSession().insert(nameSpace + "createTable1", map);

        int re2= super.getSqlSession().insert(nameSpace + "createTable2", map);

        return re1+re2;

    }

}

<!--controller-->

@Controller

@RequestMapping("/home/city")

public class CityController {

    private Log logger = LogFactory.getLog(CityController.class);

    @Resource

    private CityDAO cityDAO;

    @RequestMapping("/getAddress/0")

    @ResponseBody

    public Object getAddress(@RequestBody Map<String, Object> params)  {

        try {

            cityDAO.createTable(params);

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

return null;

}

}

AOP