springboot注解方式实现aop及常规方式
本文介绍springboot实现aop的两种方式
首先需要引入对应依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.9.1</version></dependency>
在启动类上面加上注解
@EnableAspectJAutoProxy
其实不加这个注解也可以,aop照样会生效,我们查看spring-boot-autoconfigure的依赖,查看spring.factories文件会发现以下配置
然后查看AopAutoConfiguration类会发现,当yml没有对应配置时,默认为true
下面展示常规方式实现aop的示例:
/*** @Description* @Author maruko* @Date 2022/11/22 17:04* @Version 1.0*/
@Aspect
@Component
public class AspectTest {@Pointcut("execution(* com.zjf.demo.controller.UserController.*(..))||execution(* com.zjf.demo.controller.KafkaController.*(..))")public void pointExpression() {}@Before("pointExpression()")public void before(JoinPoint joinPoint) {// System.err.println(joinPoint.toString());
// Object[] args = joinPoint.getArgs();
// for (Object arg : args) {// System.err.println(arg);
// }// System.err.println(joinPoint.getSignature().toLongString());
// System.err.println(joinPoint.getSignature().toShortString());
// System.err.println(joinPoint.getSignature().getName());
// System.err.println(joinPoint.getSignature().toString());
// System.err.println(joinPoint.getKind());
// System.err.println(joinPoint.getTarget().toString());
// System.err.println(joinPoint.getThis().toString());
// System.err.println(joinPoint.getStaticPart());
// System.err.println(joinPoint.getSourceLocation());
// System.err.println(joinPoint.toLongString());
// System.err.println(joinPoint.toShortString());System.err.println("前置通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()));}// @After("execution(* com.zjf.demo.controller.UserController.*(..))")public void after(JoinPoint joinPoint) {System.err.println("后置通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()));}// @AfterReturning(pointcut = "execution(* com.zjf.demo.controller.UserController.*(..))", returning = "result")public void afterReturning(JoinPoint joinPoint, Object result) {System.err.println("返回通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()) + " 返回结果为:" + result);}// @AfterThrowing(value = "execution(* com.zjf.demo.controller.UserController.*(..))", throwing = "exception")public void afterThrowing(JoinPoint joinPoint, Exception exception) {System.err.println("异常通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()) + " 异常为:" + exception);}// @Around("execution(* com.zjf.demo.controller.UserController.*(..))")public Object aroundMethod(ProceedingJoinPoint joinPoint) {String methodName = joinPoint.getSignature().getName();List<Object> args = Arrays.asList(joinPoint.getArgs());Object result = null;try {//前置通知System.err.println("前置通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()));result = joinPoint.proceed();//返回通知System.err.println("返回通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()) + " 返回结果为:" + result);} catch (Throwable e) {// 异常通知System.err.println("异常通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()) + " 异常为:" + e);throw new RuntimeException(e);}//后置通知System.err.println("后置通知,方法名为:" + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()));return result;}}
注解方式实现如下:
首先定义注解:
@Target(ElementType.METHOD) //定义注解的使用范围为方法
@Retention(RetentionPolicy.RUNTIME )
public @interface AopAnnotation {}
示例如下:
/**
@Description
@Author maruko
@Date 2022/11/23 9:33
@Version 1.0
*/
@Aspect
@Component
@Order(1)
public class AspectAnnotationTest {@Pointcut(“@annotation(com.zjf.demo.annotation.AopAnnotation)”)
public void pointExpression() {}
@Before(“pointExpression()”)
public void before(JoinPoint joinPoint) {
// System.err.println(joinPoint.toString());
// Object[] args = joinPoint.getArgs();
// for (Object arg : args) {
// System.err.println(arg);
// }
// System.err.println(joinPoint.getSignature().toLongString());
// System.err.println(joinPoint.getSignature().toShortString());
// System.err.println(joinPoint.getSignature().getName());
// System.err.println(joinPoint.getSignature().toString());
// System.err.println(joinPoint.getKind());
// System.err.println(joinPoint.getTarget().toString());
// System.err.println(joinPoint.getThis().toString());
// System.err.println(joinPoint.getStaticPart());
// System.err.println(joinPoint.getSourceLocation());
// System.err.println(joinPoint.toLongString());
// System.err.println(joinPoint.toShortString());
System.err.println(“注解方式实现,前置通知,方法名为:” + joinPoint.getSignature().getName() + " 参数为:" + Arrays.asList(joinPoint.getArgs()));
}
}
springboot注解方式实现aop及常规方式相关推荐
- Spring中的5种Aop常见应用方式(扫描注解方式)
转载于:https://zhuanlan.zhihu.com/p/103236714 提到Aop,不得不提的那就是动态代理:关于动态代理,可以参考前面写过的文章 加耀:浅谈动态代理zhuanlan. ...
- 第五章 Spring进阶-注解方式实现AOP(1)
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 徒弟:师傅,我 ...
- (转)使用Spring的注解方式实现AOP的细节
http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...
- (转)使用Spring的注解方式实现AOP入门
http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...
- Spring5学习(七):注解方式进行AOP操作 及 多种通知类型的测试
先来介绍一下AOP操作中的几个术语: 1.连接点:指类里面可以被增强的方法 2.切入点:指实际被增强的方法 3.通知:指实际增强的逻辑部分 4.切面:把通知应用到切入点的过程 Spring框架一般都是 ...
- 十、springboot注解式AOP(@Aspect)统一日志管理
springboot注解式AOP(@Aspect)统一日志管理 简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功 ...
- SpringBoot解决跨域的5种方式
本文来说下SpringBoot中实现跨域的5种方式. 文章目录 什么是跨域 java解决CORS跨域请求的方式 返回新的CorsFilter(全局跨域) 重写WebMvcConfigurer(全局跨域 ...
- Springboot实现登录拦截的三种方式
文章目录 1. 登录认证 1.1 介绍 1.2 方式 1.3 扩展 2. 实现 2.1 项目结构以及前置准备 2.2 过滤器实现登录拦截 2.3 拦截器实现登录拦截 2.4 AOP+自定义注解实现 2 ...
- SpringBoot静态获取 bean的三种方式,你学会了吗?
欢迎关注方志朋的博客,回复"666"获面试宝典 来源:blog.csdn.net/showchi/article/details/97005720 注意:调用者要被spring管理 ...
最新文章
- TCP,IP数据包结构
- Numpy中的random模块中的seed方法的作用
- python语言中文社区-python的汉语
- mongoDB删除某个字段(key)
- Python笔记:日期时间获取与转换
- python中循环遍历字典
- ​【Python】Python中的经典时间序列预测模型总结
- 动态链接库 仅有.dll文件时候的使用方法
- win10系统预览体验计划错误代码0x800bfa19怎么办
- c#的winform调用外部exe作为子窗体
- mysql old key files_mysql出现“Incorrect key file for table”解决办法
- C++ Vector详解
- 青出于蓝胜于蓝-(树状数组+DFS序)
- 靶机渗透之Typhoon实战
- php插入数据含有特殊符号的处理方法
- Halcon矩阵(Matrix)算子详解
- python白噪声检验结果查询_使用python实现时间序列白噪声检验方式
- 使用PHPstudy在Windows服务器下部署PHP系统
- 从安格视,安维到安尼
- 安东尼奥·维瓦尔第——巴洛克音乐的奇葩
热门文章
- 分析这家薄利多销的酸菜鱼面,看它的经营之道
- 【模拟经营】《模拟城市4豪华版》免安装中文版
- 飞浆AI studio人工智能课程学习(2)-Prompt优化思路|十个技巧高效优化Prompt|迭代法|Trick法|通用法|工具辅助
- WordPress自媒体网站迁移
- Linux系统启动分析
- npm 安装碰到SSL问题
- 当程序员变身为黑客,现役程序员表示:我太难了!
- 在word中给方框打勾
- SpringBoot报错Shutting down ExecutorService ‘applicationTaskExecutor‘解决方法
- 解决电脑无法通过网线直连海康摄像机的问题