springboot使用aspectJ
添加springboot-aop的starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
编写切面代码(先执行环绕方法->方法前->方法后->方法返回后,开启环绕方法时,异常方法无效)
@Aspect
@Component
public class AspectService {
//coder.rdf.mybatis.study.service包下的所有类的所有方法
@Before(value="execution(* coder.rdf.mybatis.study.service.*.*(..))")
public void before(JoinPoint joinPoint) throws Throwable {
Object[] args = joinPoint.getArgs();
String name = joinPoint.getSignature().getName();
System.out.println("方法前****切面方法**"+name+"***"+ Arrays.toString(args));
}
@After(value="execution(* coder.rdf.mybatis.study.service.*.*(..))")
public void after(JoinPoint joinPoint){
Object[] args = joinPoint.getArgs();
String name = joinPoint.getSignature().getName();
System.out.println("方法后****切面方法**"+name+"***"+ Arrays.toString(args));
}
@AfterReturning(value="execution(* coder.rdf.mybatis.study.service.*.*(..))" ,returning = "result")
public void afterRunning(JoinPoint joinPoint,Object result){
Object[] args = joinPoint.getArgs();
String name = joinPoint.getSignature().getName();
System.out.println("方法反回后****切面方法**"+name+"***"+ Arrays.toString(args)+"***"+result);
}
@Around(value="execution(* coder.rdf.mybatis.study.service.*.*(..))")
public Object rounding(ProceedingJoinPoint joinPoint) throws Throwable {
long start = Time.now();
System.out.println("环绕方法开始****");
Object proceed = null;
try {
Object[] args = joinPoint.getArgs();
proceed = joinPoint.proceed(args);
} catch (Throwable throwable) {
throw new Throwable("方法调用失败");
}
System.out.println("环绕方法结束****");
long end = Time.now();
System.out.println("****方法执行时间**"+(start-end));
return proceed;
}
@AfterThrowing(value = "execution (* coder.rdf.mybatis.study.service.*.*(..))", throwing = "e")
public void throwException(JoinPoint joinPoint, Exception e) {
String methodName = joinPoint.getSignature().getName();
System.out.println("异常方法调用:" + methodName + " 异常信息" + e);
}
}
springboot使用aspectJ的更多相关文章
- 梳理一下我理解的aop
在看了很多网上的资料和记录之后,我大概捋了下SpringAOP的各种阶段: 基本的advice编程,利用ProxyFactory拿代理类 利用spring把ProxyFactory,advice等be ...
- aop详解与实战
1. 什么是AOP aop:面向切面编程.采用横向机制. oop:面向对象编程.采用纵向机制. AOP,面向切面编程.就是通过某个切入点(比如方法开始.结束)向某个切面(被切的对象)切入环绕通知(需要 ...
- java注解@Transactional事务类内调用不生效问题及解决办法
@Transactional 内部调用例子 在 Spring 的 AOP 代理下,只有目标方法由外部调用,目标方法才由 Spring 生成的代理对象来管理,这会造成自调用问题.若同一类中的其他没有@T ...
- SpringBoot下使用AspectJ(CTW)下不能注入SpringIOC容器中的Bean
SpringBoot下使用AspectJ(CTW)下不能注入SpringIOC容器中的Bean 在SpringBoot中开发AspectJ时,使用CTW的方式来织入代码,由于采用这种形式,切面Bean ...
- SpringBoot实例
7player 7号球员 -- Show Time !跳至内容 首发 左边锋 技术流 外援 教练 7号 基于SpringBoot + Mybatis实现SpringMVC Web项目[原创] 目录 [ ...
- 第九章 springboot + mybatis + 多数据源 (AOP实现)
在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...
- 【spring-boot】spring aop 面向切面编程初接触--切点表达式
众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...
- Springboot基础篇
Springboot可以说是当前最火的java框架了,非常适合于"微服务"思路的开发,大幅缩短软件开发周期. 概念 过去Spring充满了配置bean的xml文件,随着spring ...
- SpringBoot 注解事务声明式事务
转载请注明: http://www.cnblogs.com/guozp/articles/7446477.html springboot 对新人来说可能上手比springmvc要快,但是对于各位从sp ...
随机推荐
- 【mq读书笔记】消息拉取长轮训机制(Broker端)
RocketMQ并没有真正实现推模式,而是消费者主动想消息服务器拉取消息,推模式是循环向消息服务端发送消息拉取请求. 如果消息消费者向RocketMQ发送消息拉取时,消息未到达消费队列: 如果不启用长 ...
- 新手上路A4——多JDK环境变量的配置
目录 配置单个JDK的方法 配置2+JDK的方法 方法 补充 检查JDK版本是否切换成功 前面讲了如何选择Java版本. 以及JDK8和JDK11的下载安装配置 有想法的人就开始发动他们优秀的小脑袋瓜 ...
- MySQL二进制文件(binlog)
二进制文件(binlog)记录对MySQL数据库执行更改的所有操作,但不包括SELECT和SHOW这类操作,因为这类操作没有改变数据. 为什么会有binlog? 首先 binlog 是 Server ...
- MongoDB 分片集群配置
本文测试环境为 CentOS 7 和 MongoDB 最新版 (4.0.12) 使用 root 操作 (实际操作中使用非 root 账户启动报错) 零.服务器分配 服务器 102 服务器 103 服务 ...
- python3 通过 pybind11 使用Eigen加速代码
python是很容易上手的编程语言,但是有些时候使用python编写的程序并不能保证其运行速度(例如:while 和 for),这个时候我们就需要借助c++等为我们的代码提速.下面是我使用pybind ...
- 使用PyQt(Python+Qt)+动态编译36行代码实现的计算器
PyQt是基于跨平台的图形界面C++开发工具Qt加Python包装的一个GPL软件(GPL是GNU General Public License的缩写,是GNU通用公共授权非正式的中文翻译),Qt基于 ...
- linux进程管理(linux命令安装、进程生命周期、进程状态)
1 linux下如何杀掉进程 1)找到包名所占用的端口: ps aux | grep cbs_portal-1.0.1.jar(包名) 2)杀掉进程: kill 10942(端口号) PS: //-- ...
- ORACLE PRAGMA AUTONOMOUS_TRANSACTION 自治事务 单独提交某一段操作
个人使用示例: CREATE OR REPLACE PROCEDURE logs(p_remark VARCHAR2, p_log CLOB) AS PRAGMA AUTONOMOUS_TRANSAC ...
- 两种方式简单免杀ew
1.资源操作法 使用工具: Restorator 2018 BeCyIconGrabber 首先我们从github下载ew使用360进行查杀 打开Restorator 将ew拖入,右键添加资源 选择图 ...
- 认识css常见的hack
一.认识css hack CSS Hack只要是来解决浏览器局部的兼容性问题,主要是因为每个浏览器对css的解析各不相同,导致输出到页面的效果的差异: 二.css hack的三种常见形式:css属性h ...