AOP中环绕通知的书写和配置
package com.hope.utils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.sql.SQLException;
/**
* @author newcityman
* @date 2019/11/21 - 20:52
*/
@Component("txManager")
@Aspect
public class TransactionManager {
@Autowired
private ConnectionUtils connectionUtils;
@Pointcut("execution(* com.hope.service.impl.*.*(..))")
private void pt(){ }
/**
* 开启事务
*/
public void beainTransaction(){
try {
connectionUtils.getThreadConnection().setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 提交事务
*/
public void commit(){
try {
connectionUtils.getThreadConnection().commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 事务回滚
*/
public void rollback(){
try {
connectionUtils.getThreadConnection().rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 开启事务
*/
public void release(){
try {
connectionUtils.getThreadConnection().close();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Around("pt()")
public Object aroundAdvice(ProceedingJoinPoint pjp){
Object rtValue=null;
try {
//1、获取参数
Object[] args = pjp.getArgs();
//2、开启事务
this.beainTransaction();
//3、执行方法
rtValue=pjp.proceed(args);
//4、提交事务
this.commit();
//5、返回结果
return rtValue;
} catch (Throwable e) {
//6、回滚事务
this.rollback();
throw new RuntimeException("事务提交有误,请联系管理员");
} finally {
//7、释放资源
this.release();
}
}
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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">
<!--配置spring扫描包-->
<context:component-scan base-package="com.hope"/>
<!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--连接数据库的必备信息-->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/easy"/>
<property name="user" value="root"/>
<property name="password" value="123"/>
</bean>
<!--开启spring对注解AOP的支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
}
AOP中环绕通知的书写和配置的更多相关文章
- AOP中环绕通知的写法
package com.hope.utils;import org.aspectj.lang.ProceedingJoinPoint;/** * @author newcityman * @date ...
- java中AOP的环绕通知
pom.xml <dependencies> <dependency> <groupId>org.springframework</groupId> & ...
- 在Spring aop中的propagation的7种配置的意思
<tx:method name="find*" read-only="true" propagation ="NOT_SUPPORTED&quo ...
- spring aop中的propagation的7种配置的意思
1.前言. 在声明式的事务处理中,要配置一个切面,即一组方法,如 <tx:advice id="txAdvice" transaction-manager="txM ...
- Spring AOP 中pointcut expression表达式解析及配置
Pointcut是指那些方法需要被执行”AOP”,是由”Pointcut Expression”来描述的. Pointcut可以有下列方式来定义或者通过&& || 和!的方式进行组合. ...
- spring aop中的propagation的7种配置
1.前言 在声明式的事务处理中,要配置一个切面,即一组方法,如 <tx:advice id="txAdvice" transaction-manager="txMa ...
- AOP 环绕通知 (Schema-base方式) 和 AspectJ方式在通知中获取切点的参数
环绕通知(Schema- base方式) 1.把前置通知和后置通知都写到一个通知中,组成了环绕通知 2.实现步骤: 2.1 新建一个类实现 MethodInterceptor 接口 public cl ...
- spring中的AOP 以及各种通知 配置
理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦. spring中的AOP很好地解决了 ...
- 基于XML的AOP配置(2)-环绕通知
配置方式: <aop:config> <aop:pointcut expression="execution(* com.itheima.service.impl.*.*( ...
随机推荐
- c++学习笔记(九)
引用(reference) 概念 引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字. 一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量. 用法 变量名称是变量附属在内存 ...
- hbuilder中webview调试console.log无法输出日志的问题
遇到这个问题的亲,肯定是用的模拟器来测试的,其实你只要换成真机测试就能打印了,前提是安卓系统. 有问题欢迎留言,如果你觉得这个文章对你有帮助,就请点个赞吧!
- Centos8 Docker部署ElasticSearch集群
ELK部署 部署ElasticSearch集群 1.拉取镜像及批量生成配置文件 # 拉取镜像 [root@VM-24-9-centos ~]# docker pull elasticsearch:7. ...
- Linux下编译tinyxml生成动态库
首先去到sourceforge下载tinyxml的源码,https://sourceforge.net/projects/tinyxml/?source=dlp,最新版本是2.6.2. 将下载成功的t ...
- [hdu6990]Directed Minimum Spanning Tree
模板题:在有向图中,对每一个点求以其为根的最小(外向)生成树 (当图是强连通时)可以使用朱刘算法,算法过程如下: 1.对每一个节点,选择指向该点的边权最小的边,即得到一张子图 2.任选这张子图的一个简 ...
- [loj3247]Non-Decreasing Subsequences
分治,考虑分治到[l,r]的区间(设$mid=(l+r)/2$),将询问分为两类:1.在左/右区间,直接递归下去:2.跨越中间,那么处理出两个数组:L[i][j]表示左区间在开头第i个位置,以数字j为 ...
- 【Tool】IntelliJ IDEA Ultimate2019.1 中文版 安装
IntelliJ IDEA Ultimate2019.1 2019-07-26 09:26:24 by冲冲 1. 下载 https://mp.weixin.qq.com/s/SdFQqGzMy-g ...
- 【JavaSE】异常
Java异常 2019-07-06 22:16:29 by冲冲 1. 引例 任何程序都有出错的可能.比如代码少一个分号,那么运行的结果是 java.lang.Error.比如运行 System.o ...
- Revit二次开发之获取本机已安装的Revit版本与路径
在revit安装目录下找到 RevitAddInUtility.dll 在项目中引用 使用以下方法 using Autodesk.RevitAddIns; /// <summary> // ...
- P6072 『MdOI R1』Path
考虑我们有这样操作. 我们只要维护两点在子树内和两点在子树外的异或和即可. 前者可以类似于线段树合并的trie树合并. 后者有两种做法: 一种是把dfn序翻倍:然后子树补变成了一个区间最大异或问题,可 ...