JoinPoint 可获取到切入点的相关信息。

自定义注解 @Action

package com.zhujiejoinpoint.demo;import java.lang.annotation.*;@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Action {public String value() default "this is value";public String key() default "myKey:";public int time() default 60;
}

定义AOP切点及前置处理

package com.zhujiejoinpoint.demo;import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;import java.lang.reflect.Method;@Aspect
@Component
public class Aspact {// 下边意思:只要带有 @Action 注解的方法,在执行时,check() 方法会先执行// 通过joinPoint 可以拿到切点的任何信息,同时也可以拿到方法上带有 @Action 注解的任何信息@Pointcut("@annotation(com.zhujiejoinpoint.demo.Action)")public void pointFn(){}@Before("pointFn()")public void check(JoinPoint joinPoint) {// ··· ···}
}

测试的controller

@RestController
public class test {@Action@GetMapping(value = "/get")public HashMap<String, Object> aop(String name, Integer age) {HashMap<String, Object> hashMap = new HashMap<String, Object>(){{put("name", name);put("age", age);}};return hashMap;}
}

下边主要来记录下JoinPoint 切点的方法

(1)Object[]  joinPoint.getArgs()
获取切点方法的入参列表 ,返回结果 Object[]
测试路径:http://localhost:8080/get?name=吕星辰&age=20

结果:

(2)Object joinPoint.getTarget(); 
获取切入点的目标对象,返回Object, 可以通过该对象.getClass() 方法获取类的任何信息

(3)joinPoint.getSignature()

获取封装了署名信息对象,在该对象中可以获取到目标方法名所属类的class等信息。

// 获取被@Action 修饰的方法的名字
System.out.println(joinPoint.getSignature().getName()); // aop// 获取封装了署名信息对象
Signature signature = joinPoint.getSignature();// 目标方法的名称
String className = signature.getName(); // “aop”// 目标方法所在的类 "class com.zhujiejoinpoint.demo.controller.test"
Class mClass = signature.getDeclaringType();// 目标方法所在类的类名 "test"
String simpleName = signature.getDeclaringType().getSimpleName();// 获取目标方法修饰符 “public”
String modifiers = Modifier.toString(signature.getModifiers());// 获取目标方法所在类的类名全路径 "com.zhujiejoinpoint.demo.controller.test"
String declaringTypeName = signature.getDeclaringTypeName();

(4)通过 Method  目标方法对象,可获取到目标方法的相关信息

MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();

如下边:获取到目标方法名称、目标方法所在类的全限定名称、类名等等。

Signature signature = joinPoint.getSignature();
// 获取到目标方法对象
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();// 获取目标方法的名称 “aop”
String fnName = method.getName();// 获取目标方法所在的类 "class com.zhujiejoinpoint.demo.controller.test"
Class declaringClass = method.getDeclaringClass();// 获取目标方法所在的类全路径 "com.zhujiejoinpoint.demo.controller.test"
String name = declaringClass.getName();// 获取目标方法所在的类的类名 "test"
String simpleName = declaringClass.getSimpleName();

(5)获取目标方法上的 @Action注解 中的相关信息

这块非常重要,因为:可以通过自定义注解,来实现某些功能,比如:编写一个限流的自定义注解,注解中定义一些属性方法time(s)、count(次数),在需要限流的controller方法上添加该注解,执行该方法时,使用aop获取目标方法上的自定义注解,拿到定义的值,判断多少秒内限流的次数等等。

// 获取目标方法对象
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();// 获取目标方法上的注解对象
Action annotation = method.getAnnotation(Action.class);
// 通过注解对象 来获取注解中的属性值,自定义注解定义了哪一些属性,直接取就行啦
String value = annotation.value(); // “this is value”

在实际开发中还需要判断下,是否有该注解:

/*** 是否存在注解,如果存在就获取*/
private RateLimiter getAnnotationRateLimiter(JoinPoint joinPoint)
{Signature signature = joinPoint.getSignature();MethodSignature methodSignature = (MethodSignature) signature;Method method = methodSignature.getMethod();if (method != null){return method.getAnnotation(RateLimiter.class);}return null;
}

基本就这么多,没记录的,开发中用到时在补充。

补充(2022-02-19 10:46):

以上边 自定义 Action 注解为例,切点的简写:

// 普通写法
@Pointcut("@annotation(com.zhujiejoinpoint.demo.Action)")
public void targetBeforeFn() {}@Before("targetBeforeFn()")
public void beforeFn (JoinPoint joinPoint){ // 执行切点的前置方法// ···
}
// 简写
@Before("@annotation(selfDefaultName)")
public void beforFn(JoinPoint joinPoint, Active selfDefaultName) {// ···
}

很轻松的获取到注解中的值

@Aspect
@Component
public class acept {@Before("@annotation(selfDefaultValue)")public void targetBeforeFn(JoinPoint joinPoint, Action selfDefaultValue) {Object[] args = joinPoint.getArgs();System.out.println("参数:"+JSON.toJSONString(args));// 获取注解中的值System.out.println(selfDefaultValue.value());  // this is valueSystem.out.println(selfDefaultValue.key()); // myKey:System.out.println(selfDefaultValue.time()); // 60}
}

AOP JoinPoint相关推荐

  1. Spring AOP示例教程 - Aspect,Advice,Pointcut,JoinPoint,Annotations,XML Configuration

    Spring AOP示例教程 - Aspect,Advice,Pointcut,JoinPoint,Annotations,XML Configuration Spring Framework是基于两 ...

  2. aop中joinpoint_Spring AOP示例教程–方面,建议,切入点,JoinPoint,注释,XML配置...

    aop中joinpoint Spring Framework is developed on two core concepts – Dependency Injection and Aspect O ...

  3. spring的AOP配置之XML方式

    AOP概念 ⚫ AOP(Aspect Oriented Programing)面向切面编程,一种编程范式,隶属于软工范畴,指导开发者如 何组织程序结构 ⚫ AOP弥补了OOP的不足,基于OOP基础之上 ...

  4. Spring 框架 AOP 的总结

    一:帝国之军-AOP 1:案例分析 ![事务案例](https://img-blog.csdn.net/2018040518063398?watermark/2/text/aHR0cHM6Ly9ibG ...

  5. spring boot 整合web开发(二)

    目录 自定义错误页 CORS支持(前端跨域请求) 拦截器HandlerInterceptor 启动系统任务 springboot配置AOP 整合servlet.filter.listener 下图为本 ...

  6. 小汤学编程之JavaEE学习day10——Spring

    一.Spring简介 1.特点     2.核心特性     3.两大核心 二.Spring环境搭建 1.导包     2.准备数据库和表.实体类     3.定义dao层接口和接口映射文件      ...

  7. springboot+vue学习

    书籍:Spring Boot + Vue全栈开发   作者:王松 第一章:Spring Boot入门 1.1 简介 1.1.1 优势 快速构建项目 通用性配置 内嵌服务器 1.2 第一个spring ...

  8. ejb客户端的三种调用方法,以及InitialContext lookup后的jndi对象在服务重启后缓存失效的问题

    1.第一种调用方法: Properties p = new Properties();p.setProperty("java.naming.factory.initial", &q ...

  9. eclipse aop连接点joinpoint方法点不出来_(面试必备)你必须要懂的Spring-Aop

    在实际研发中,Spring是我们经常会使用的框架,毕竟它们太火了,也因此Spring相关的知识点也是面试必问点,今天我们就大话Aop. 特地在周末推文,因为该篇文章阅读起来还是比较轻松诙谐的,当然了, ...

最新文章

  1. 2022-2028年中国自热米饭市场竞争策略及行业投资潜力预测报告
  2. 好污!杜蕾斯:一份2017年度账单和床上总结求认领...
  3. BigData:根据最新2018人工智能行业创新企业Top100名单,绘制AI行业VC最AI的热门领域(投资领域)
  4. 结合批处理功能,配置SQL Server 2005,使其打开远程连接功能
  5. python datetime计算时间差_用datetime计算时间差
  6. pandas的dataframe节省内存
  7. 属性提取器:获取ListView即时更新其元素的最佳方法
  8. pycharm不同py文件共享参数_PyCharm安装笔记
  9. 2020年生活服务业新业态和新职业从业报告
  10. 如何从Debian 9 Stretch 升级到 Debian 10 Buster(升级Debian9图文教程)
  11. 计算机网络——OSI参考模型和网络的排错
  12. 映射文件中增删改查标签中的parameterType和resultType
  13. 神操作!员工索要工资遭遇“假转账”:转了生气又撤销
  14. 诊断域帐号被锁定的原因
  15. Node.js:POST请求、文件上传
  16. 计算机二级教程第十章答案,2012年计算机二级VFP考试第十章课后练习题及答案...
  17. android 学习之SurfaceView
  18. 年终工作总结汇报和述职报告ppt模板,内含范文可参考,精选20套可下载
  19. 【Hexo搭建个人博客】:yilia主题配置(二) - 背景图片
  20. FL Studio 蜕变发展史及FL Studio21如何带你进入AI编曲时代

热门文章

  1. 穿山甲和广点通激励视频广告打开失败的异常处理
  2. PHP合并数组几种方式总结及数组去重
  3. 【机器学习自学笔记6】高斯混合模型(GMM)
  4. 浅谈Java异常及其编译时异常和运行时异常的区别
  5. Flink的各种Transformations 代码例子-- Java和Scala实现
  6. MQ测试:发出第一条MQ消息
  7. CVE-2017-11882漏洞分析报告
  8. ext4文件系统综述
  9. Spring getBean流程
  10. Android 组件化架构概要,熬夜整理Android高频面试题