spring aop事务配置
Posted xiaolei2017
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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: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事务配置的主要内容,如果未能解决你的问题,请参考以下文章
spring5 源码深度解析----- Spring事务 是怎么通过AOP实现的?(100%理解Spring事务)