码迷,mamicode.com
首页 > 编程语言 > 详细

spring aop事务配置

时间:2018-04-28 15:44:21      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:log   nal   word   log4   http   ice   ram   insert   auto   

<?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:jbbc="http://www.springframework.org/schema/jdbc" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd  
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <!-- mysql config -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${juchao.db.driverclass}" />
        <property name="url" value="${juchao.db.jdbcurl}" />
        <property name="username" value="${juchao.db.username}" />
        <property name="password" value="${juchao.db.password}" />
        <property name="maxActive" value="100" />
        <property name="maxWait" value="1000" />
        <property name="poolPreparedStatements" value="true" />
        <property name="defaultAutoCommit" value="true" />
        <property name="validationQuery" value="SELECT 1+1" />
        <property name="testOnBorrow" value="true" />
        <property name="testOnReturn" value="true" />
        <property name="testWhileIdle" value="true" />
    </bean>


    <!-- 配置事务管理器, -->
    <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="insert*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--
         transaction-manager="txAdvice"  引入Spring事务管理类
         name="delect*" 要执行(insert*开头)的方法名,propagation="REQUIRED"有transaction状态下执行。
                  如果当前没有transaction,就创建新的transaction。
    -->

    <!-- 配置AOP切面 -->
    <aop:config>
        <!-- 配置切入点 -->
        <aop:pointcut expression="execution(* com.csf.jdbctemplate.dao..*.*(..))" id="daoMethod"/>
        <!-- 配置切面 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethod"/>
    </aop:config>
    <!--
        expression="execution(* com.csf.jdbctemplate.dao.*.*(..))" 第一个*代表所有的返回值类型,
        com.csf.jdbctemplate.dao代表包名,第二个*带表所有的类,第三个*代表所有的方法,(..)代表方法的参数。
    -->
    
</beans>


----------------------------------------------------------------------------------------------------------------
package com.csf.jdbctemplate.dao;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.Date;

/**
 * Created by fenglei.ma on 2018/4/27. 9:03
 */
@Repository
public class DictEntityDao {

    private static final Logger logger = Logger.getLogger(DictEntityDao.class);

    @Autowired
    // 使用spring 的 jdbcTemplate 进行查询
    private JdbcTemplate jdbcTemplate;

    
    // 模拟Aop事务管理  这里不能使用try catch
    public String insertEntityAop() {    
        for(int i=0; i<4; i++) {
             Long maxId = maxId();
             String sql = "insert into dict_xiaoma(id,name,word,state,type,crt,upu,cru,lastmodified_by,upt) values(?,?,?,?,?,?,?,?,?,?)";
             int numrow = jdbcTemplate.update(sql, new Object[]{maxId + 1, "重复Aop模拟3", "再次模拟的", 6, 6, new Date(), "SO11111111","75281","787878787878",new Date()});     
             if(i==1) {
                     String s = null;
                     logger.error("Error===================================>模拟aop报错");
                     s.equals(null);
             }
        }         
        return " 添加成功 ";
    }

}


----------------------------------------------------------------------------------------------------------------
<!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.6.RELEAS</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.6.RELEAS</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.6.RELEAS</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.6.RELEAS</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.6.RELEAS</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.6.RELEAS</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.6.RELEAS</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.6.RELEAS</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.6.RELEAS</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.1.6.RELEAS</version>
        </dependency>
<!-- End of Spring dependencies -->

 

spring aop事务配置

标签:log   nal   word   log4   http   ice   ram   insert   auto   

原文地址:https://www.cnblogs.com/xiaolei2017/p/8967656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!