JoinPoint的用法
JoinPoint 对象
JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象.
常用api:
方法名 | 功能 |
---|---|
Signature getSignature(); | 获取封装了署名信息的对象,在该对象中可以获取到目标方法名,所属类的Class等信息 |
Object[] getArgs(); | 获取传入目标方法的参数对象 |
Object getTarget(); | 获取被代理的对象 |
Object getThis(); | 获取代理对象 |
ProceedingJoinPoint对象
ProceedingJoinPoint对象是JoinPoint的子接口,该对象只用在@Around的切面方法中,
添加了
Object proceed() throws Throwable //执行目标方法
Object proceed(Object[] var1) throws Throwable //传入的新的参数去执行目标方法
两个方法.
Demo
切面类
@Aspect
@Component
public class aopAspect {/*** 定义一个切入点表达式,用来确定哪些类需要代理* execution(* aopdemo.*.*(..))代表aopdemo包下所有类的所有方法都会被代理*/@Pointcut("execution(* aopdemo.*.*(..))")public void declareJoinPointerExpression() {}/*** 前置方法,在目标方法执行前执行* @param joinPoint 封装了代理方法信息的对象,若用不到则可以忽略不写*/@Before("declareJoinPointerExpression()")public void beforeMethod(JoinPoint joinPoint){System.out.println("目标方法名为:" + joinPoint.getSignature().getName());System.out.println("目标方法所属类的简单类名:" + joinPoint.getSignature().getDeclaringType().getSimpleName());System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName());System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));//获取传入目标方法的参数Object[] args = joinPoint.getArgs();for (int i = 0; i < args.length; i++) {System.out.println("第" + (i+1) + "个参数为:" + args[i]);}System.out.println("被代理的对象:" + joinPoint.getTarget());System.out.println("代理对象自己:" + joinPoint.getThis());}/*** 环绕方法,可自定义目标方法执行的时机* @param pjd JoinPoint的子接口,添加了* Object proceed() throws Throwable 执行目标方法* Object proceed(Object[] var1) throws Throwable 传入的新的参数去执行目标方法* 两个方法* @return 此方法需要返回值,返回值视为目标方法的返回值*/@Around("declareJoinPointerExpression()")public Object aroundMethod(ProceedingJoinPoint pjd){Object result = null;try {//前置通知System.out.println("目标方法执行前...");//执行目标方法//result = pjd.proeed();//用新的参数值执行目标方法result = pjd.proceed(new Object[]{"newSpring","newAop"});//返回通知System.out.println("目标方法返回结果后...");} catch (Throwable e) {//异常通知System.out.println("执行目标方法异常后...");throw new RuntimeException(e);}//后置通知System.out.println("目标方法执行后...");return result;}
}
被代理类
/*** 被代理对象*/
@Component
public class TargetClass {/*** 拼接两个字符串*/public String joint(String str1, String str2) {return str1 + "+" + str2;}
}
测试类
public class TestAop {@Testpublic void testAOP() {//1、创建Spring的IOC的容器ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:bean.xml");//2、从IOC容器中获取bean的实例TargetClass targetClass = (TargetClass) ctx.getBean("targetClass");//3、使用beanString result = targetClass.joint("spring","aop");System.out.println("result:" + result);}
}
输出结果
目标方法执行前...
目标方法名为:joint
目标方法所属类的简单类名:TargetClass
目标方法所属类的类名:aopdemo.TargetClass
目标方法声明类型:public
第1个参数为:newSpring
第2个参数为:newAop
被代理的对象:aopdemo.TargetClass@4efc180e
代理对象自己:aopdemo.TargetClass@4efc180e
目标方法返回结果后...
目标方法执行后...
result:newSpring+newAop
参考文章:
http://blog.csdn.net/ochangwen/article/details/52557724
http://blog.csdn.net/a9529lty/article/details/7031070
JoinPoint的用法相关推荐
- SpringAop中JoinPoint对象的使用方法
JoinPoint的用法 JoinPoint 对象 JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoi ...
- Java学习之「Spring + AspectJ 」
目录 ■前言 ■省略部分 ■代码构造 ■运行效果 ■具体代码 AspectTest MyAspect ★★★ @Aspect AppConf ★★★ @EnableAspectJAutoPro ...
- spring中AOP(面向切面编程)
spring中AOP(面向切面编程) 面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是spring框架中的一个重要内容 ...
- 02.springMVC设计思想及springAOP
MVC设计思想 历史问题:如果将大量的代码都写到一个方法中,后期维护不易 MVC说明: 1.M Model 数据层 数据处理/数据的持久化 2.V View 视图层 前端页面相关的内容(看到的内容) ...
- Spring框架的基本使用
一.前言 Spring框架基础部分内容过于很长,分为几篇文章进行巩固学习记录. 1.Spring是什么 Spring框架是由于软件开发的复杂性而创建的.Spring使用的是基本的JavaBean来完成 ...
- Spring面向切面编程-AOP详解
文章目录 前言 介绍AOP 一.实现AOP 1.1.全注解形式实现AOP 前提准备(引入jar包) 实现AOP(五种通知) 二.认识JoinPont与ProceedingJoinPoint 2.1.初 ...
- Spring AOP 简介以及简单用法
Spring AOP 简介以及简单用法 如果你去面试java开发, 那么Spring的AOP和DI几乎是必问的问题. 那么AOP是什么呢? 一. AOP 所谓Aop就是 Aspect-Oriented ...
- SpringAOP中通过JoinPoint获取值,并且实现redis注解
在Java8之前,代码编译为class文件后,方法参数的类型固定,但是方法名称会丢失,方法名称会变成arg0.arg1-..在Java8开始可以在class文件中保留参数名 public void t ...
- Spring AOP用法
软件152 杨浩艺 Spring AOP Java web 环境搭建 Java web 项目搭建 Java Spring IOC用法 spring提供了两个核心功能,一个是IoC(控制反转),另外一个 ...
最新文章
- STM32 基础系列教程 34 - Lwip_tcp_server
- Heartbeat+ipvsadm+ldirectord组建linux高可用集群
- 基于web的甘特图,易度甘特图edogantt!
- 信息学奥赛一本通(2042:【例5.10】稀疏矩阵)
- 新修订未成年人保护法6月1日正式实施
- K-means 聚类算法的图像区域分割
- 生物信息Python-从入门到精通?
- android读取mysql数据库文件_Android开发系列(十七):读取assets目录下的数据库文件...
- ashx 使用Session
- android之App widget实际应用Demo
- [unity]网游中实现资源动态加载
- java 审计日志_审计日志的实现
- 任务一:统计基础知识-样本与总体、均值与方差、二项分布、泊松分布
- matter.js学习笔记(八)--Composites.newtonsCradle()制造牛顿摆
- 复杂问题的知识问答技术介绍
- Cadence Allegro PCB设计88问解析(四) 之 Allegro中快捷键Funckey与alias设置
- iw在java中什么意思,请问程序里piw和iw有什么区别
- Windows系统移动/复制安装(和原来的系统无丝毫差别)
- 我的世界手机版javaui材质包_教你然后用手机玩电脑版我的世界模组?
- 百度网盘 下载工具整理