博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring基于注解和基于xml配置文件大全
阅读量:3908 次
发布时间:2019-05-23

本文共 1791 字,大约阅读时间需要 5 分钟。

基于注解的事务管理配置文件:

(用 切面类 去模拟的)

切面类的代码如下:

package com.yidongxueyuan.spring.utils;import java.sql.Connection;import java.sql.SQLException;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;@Component("TransactionManager")//告知spring该类是一个切面类@Aspect/** * 定义和事务相关的所有的方法:  * 事物的开启 *///设置一个通用的切点表达式public class TransactionManager {	private static ThreadLocal
tl = new ThreadLocal
(); public static Connection getConnection(){ Connection conn = tl.get(); if(conn == null){ conn = C3P0Util.getConnection(); tl.set(conn); } return conn; } @Pointcut(value="execution( * com.yidongxueyuan.spring.service.impl.AccountServiceImpl.*(..))") private void point1(){} @Before(value="point1()") public static void startTransaction(){ try { Connection conn = getConnection(); conn.setAutoCommit(false); } catch (SQLException e) { e.printStackTrace(); } } @AfterReturning(value="point1()") public static void commit(){ try { Connection conn = getConnection(); conn.commit(); } catch (SQLException e) { e.printStackTrace(); } } @AfterThrowing(value="point1()",throwing="e") public static void rollback(){ try { Connection conn = getConnection(); conn.rollback(); } catch (SQLException e) { e.printStackTrace(); } } //释放链接: public static void release() { Connection conn = getConnection(); try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } }}

转载地址:http://czqen.baihongyu.com/

你可能感兴趣的文章
Python integer ranges
查看>>
Python - Search Insert Position
查看>>
Find a Peak Element
查看>>
Find the Minimum Element in A sorted and Rotated Array
查看>>
BAT Levels
查看>>
Netflix Architecture
查看>>
Mobile architecture
查看>>
Why Netflix chose NGINX
查看>>
Division Operators
查看>>
Bitwise operation and usage
查看>>
What are bitwise operators?
查看>>
Python bitwise operator
查看>>
Recursion Vs Iteration
查看>>
Python List Pop
查看>>
recursion versus iteration
查看>>
Ansi,UTF8,Unicode,ASCII编码的区别
查看>>
取模 and 求余
查看>>
IM Architecture
查看>>
Spark Intro
查看>>
Apache Kafka
查看>>