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 ...
随机推荐
- 生僻的mysql
1.show table status like 'user' 2.alter table mytableEngine=InnoDB 需要执行很长时间,mysql会按行将数据从原表复制到一张新的表中, ...
- CentOS下关于集群同步/LB/HA 的尝试
Zookepper 集群同步 下载解压 wget http://apache.fayea.com/zookeeper/stable/zookeeper-3.4.8.tar.gz tar xvf zoo ...
- 「考试」CSP-S 2020
乱扯 爆炸的过程是这样的 写了\(2.5h\)的\(T1\)过不去大样例,自闭了 决定调\(T2\)然后过了样例但事实上写的完全是假的 这个时候突然\(T1\)灵光一闪就没再看\(T2\)了 然后就一 ...
- Beta冲刺随笔——Day_Seven
这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 团队进行Beta冲刺 作业正文 正文 其他参考文献 无 今日事今日毕 林涛: ...
- 极简python教程:快速入门好方法
大家好,我是测试奇谭的作者风风. 其实很久之前,就有身边的同事或者网友让我分享一些关于python编程语言的快速教程,他们的痛点同大多数自学编程语言的人一样,遇到了这些问题: 网络上的信息太多,良莠不 ...
- opencv-python imread、imshow浏览目录下的图片文件
☞ ░ 前往老猿Python博文目录 ░ 一.几个知识点 1.1.使用Python查找目录下的文件 具体请参考<Python正则表达式re模块和os模块实现文件搜索模式匹配>. 1.2.o ...
- Python中判断字符串是否为数字、字母、标识符、浮点数、大小写、可打印的方法
1.判断s是否都是大写字母:s.isupper(): 2.判断s是否都是小写字母:s.islower(): 3.判断s中的每个单词首字母是否都是大写字母且其他位置无大写字母:s.istitle(),要 ...
- 第15.15节 PyQt(Python+Qt)入门学习:Designer的menu菜单、toolBar工具栏和Action动作详解
老猿Python博文目录 老猿Python博客地址 一.引言 Qt Designer中的部件栏并没有菜单.toolBar以及Action相关的部件,仅在MainWindow类型窗口提供了menu.to ...
- Microsoft工具之Disk2vhd
Official documents:https://docs.microsoft.com/zh-cn/sysinternals/downloads/disk2vhd 1.Introduction D ...
- CNVD漏洞证书(1)
之前申请了CNVD原创漏洞,踩了坑,记录一下 有很多师傅写过相关的文章: https://blog.csdn.net/qq1124794084/article/details/82657840 htt ...