User-Profile-Image
hankin
  • 5
  • Java
  • Kotlin
  • Spring
  • Web
  • SQL
  • MegaData
  • More
  • Experience
  • Enamiĝu al vi
  • 分类
    • Zuul
    • Zookeeper
    • XML
    • WebSocket
    • Web Notes
    • Web
    • Vue
    • Thymeleaf
    • SQL Server
    • SQL Notes
    • SQL
    • SpringSecurity
    • SpringMVC
    • SpringJPA
    • SpringCloud
    • SpringBoot
    • Spring Notes
    • Spring
    • Servlet
    • Ribbon
    • Redis
    • RabbitMQ
    • Python
    • PostgreSQL
    • OAuth2
    • NOSQL
    • Netty
    • MySQL
    • MyBatis
    • More
    • MinIO
    • MegaData
    • Maven
    • LoadBalancer
    • Kotlin Notes
    • Kotlin
    • Kafka
    • jQuery
    • JavaScript
    • Java Notes
    • Java
    • Hystrix
    • Git
    • Gateway
    • Freemarker
    • Feign
    • Eureka
    • ElasticSearch
    • Docker
    • Consul
    • Ajax
    • ActiveMQ
  • 页面
    • 归档
    • 摘要
    • 杂图
    • 问题随笔
  • 友链
    • Spring Cloud Alibaba
    • Spring Cloud Alibaba - 指南
    • Spring Cloud
    • Nacos
    • Docker
    • ElasticSearch
    • Kotlin中文版
    • Kotlin易百
    • KotlinWeb3
    • KotlinNhooo
    • 前端开源搜索
    • Ktorm ORM
    • Ktorm-KSP
    • Ebean ORM
    • Maven
    • 江南一点雨
    • 江南国际站
    • 设计模式
    • 熊猫大佬
    • java学习
    • kotlin函数查询
    • Istio 服务网格
    • istio
    • Ktor 异步 Web 框架
    • PostGis
    • kuangstudy
    • 源码地图
    • it教程吧
    • Arthas-JVM调优
    • Electron
    • bugstack虫洞栈
    • github大佬宝典
    • Sa-Token
    • 前端技术胖
    • bennyhuo-Kt大佬
    • Rickiyang博客
    • 李大辉大佬博客
    • KOIN
    • SQLDelight
    • Exposed-Kt-ORM
    • Javalin—Web 框架
    • http4k—HTTP包
    • 爱威尔大佬
    • 小土豆
    • 小胖哥安全框架
    • 负雪明烛刷题
    • Kotlin-FP-Arrow
    • Lua参考手册
    • 美团文章
    • Java 全栈知识体系
    • 尼恩架构师学习
    • 现代 JavaScript 教程
    • GO相关文档
    • Go学习导航
    • GoCN社区
    • GO极客兔兔-案例
    • 讯飞星火GPT
    • Hollis博客
    • PostgreSQL德哥
    • 优质博客推荐
    • 半兽人大佬
    • 系列教程
    • PostgreSQL文章
    • 云原生资料库
    • 并发博客大佬
Help?

Please contact us on our email for need any support

Support
    首页   ›   Spring   ›   正文
Spring

Spring AOP中JoinPoint的用法

2020-03-11 11:30:29
1211  0 0

阅读完需:约 2 分钟

JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象. joinpoint.getargs():获取带参方法的参数

1.joinpoint.getargs():获取带参方法的参数

注:就是获取组件中test方法中的参数,如果test方法中有多个参数,那么这个方法机会返回多个参数.想要哪个就通过for循环加上判断来筛选

2.joinpoint.getTarget():.获取他们的目标对象信息

3..joinpoint.getSignature():(signature是信号,标识的意思):获取被增强的方法相关信息.其后续方法有两个

getDeclaringTypeName: 返回方法所在的包名和类名

getname() : 返回方法名

例子:

先写一个需要被拦截的实现类

@Component("carimp")
public class MyCalImp implements MyCal{

	public int mul(int i, int j) {
		// TODO Auto-generated method stub
		System.out.println("我是方法mul");
		return i-j;
	}

	public int sub(int i, int j) {
		// TODO Auto-generated method stub
		System.out.println("我是方法sub");
		return i+j;
	}

}

然后再去拦截它

@Component("jisuan")
@Aspect
public class jisuan {
	
	@Pointcut("execution(* sp.*.*(..))")
	public void pointcut(){}
	
	@Before("pointcut()")
	public void before(JoinPoint joinPoint) {
       System.out.println(Arrays.toString(joinPoint.getArgs())+" "+joinPoint.getSignature().getName());
    } 
	
	@After("pointcut()")
	public void after(JoinPoint joinPoint){
		System.out.println("the method *** ends!");
		
	}
}

@Before里面就是拦截的同时还获取了参数列表和方法名字!

结果:

public class MyTest {
	ApplicationContext context;
	
	@Before
	public void fa(){
		context=new ClassPathXmlApplicationContext("ctx.xml");
	}
	
	@Test
	public void t1(){
		MyCal c=(MyCal) context.getBean("carimp");
//		System.out.println(c.mul(1, 2));
		int x=c.mul(1, 5);
		System.out.println(x);
		int y=c.sub(5, 6);
		System.out.println(y);
	}
}

如本文“对您有用”,欢迎随意打赏作者,让我们坚持创作!

0 打赏
Enamiĝu al vi
不要为明天忧虑.因为明天自有明天的忧虑.一天的难处一天当就够了。
543文章 68评论 294点赞 594528浏览

随机文章
SpringCloud—OpenFeign(四)配置详解+Hystrix
5年前
SpringBoot—AOP的用法
5年前
PostgreSQL—如何查找TOP SQL
1年前
SpringMVC笔记—数据显示到前端(补充)
5年前
Axios的简单使用
5年前
博客统计
  • 日志总数:543 篇
  • 评论数目:68 条
  • 建站日期:2020-03-06
  • 运行天数:1927 天
  • 标签总数:23 个
  • 最后更新:2024-12-20
Copyright © 2025 网站备案号: 浙ICP备20017730号 身体没有灵魂是死的,信心没有行为也是死的。
主页
页面
  • 归档
  • 摘要
  • 杂图
  • 问题随笔
博主
Enamiĝu al vi
Enamiĝu al vi 管理员
To be, or not to be
543 文章 68 评论 594528 浏览
测试
测试
看板娘
赞赏作者

请通过微信、支付宝 APP 扫一扫

感谢您对作者的支持!

 支付宝 微信支付