常用方法
JoinPoint 对象
JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象.
1、常用api
方法名 | 功能 |
---|---|
Signature getSignature(); | 获取封装了署名信息的对象,在该对象中可以获取到目标方法名,所属类的Class等信息 |
Object[] getArgs(); | 获取传入目标方法的参数对象 |
Object getTarget(); | 获取被代理的对象 |
Object getThis(); | 获取代理对象 |
2、Signature常用方法
package org.aspectj.lang;
public interface Signature {
String toString();
String toShortString();
String toLongString();
String getName();
int getModifiers();
Class getDeclaringType();
String getDeclaringTypeName();
}
打印输出,getString是测试类的方法名,TestController是类名
joinPoint.getSignature().toString():String com.fast.web.controller.TestController.getString()
joinPoint.getSignature().toShortString():TestController.getString()
joinPoint.getSignature().toLongString():public java.lang.String com.fast.web.controller.TestController.getString()
joinPoint.getSignature().getName():getString
joinPoint.getSignature().getModifiers():1
joinPoint.getSignature().getDeclaringType():class com.fast.web.controller.TestController
joinPoint.getSignature().getDeclaringTypeName():com.fast.web.controller.TestController
冒号前面是使用的方法,后面是本次测试输出的结果。
getModifiers()方法返回int类型值表示该字段的修饰符。
其中,该修饰符是java.lang.reflect.Modifier的静态属性。
对应表如下:
PUBLIC: 1
PRIVATE: 2
PROTECTED: 4
STATIC: 8
FINAL: 16
SYNCHRONIZED: 32
VOLATILE: 64
TRANSIENT: 128
NATIVE: 256
INTERFACE: 512
ABSTRACT: 1024
STRICT: 2048
附上被测试的类:
@RestController
public class TestController {
@Autowired
private TestDao testDao;
@RequestMapping("/test")
public String getString() {
int i = testDao.selectBase();
return String.valueOf(i);
}
}
3、joinPoint方法—— 被代理对象vs代理对象
getTarget() 被代理对象,即连接点所在的目标对象(周杰伦)
getThis()代理对象(经纪人)
两者的区别,上代码:
package com.example.worktest.aop.common;
@Aspect
@Component
public class OperationAspect {
@AfterReturning(pointcut = "within(com.example..*) && @annotation(operationLog)",//注意联调时修改包名!
returning = "result")
public void getOperationLog(JoinPoint jp, OperationLog operationLog, Object result) throws Exception {
Object getTargetResult = jp.getTarget();
Object getThisResult = jp.getThis();
System.out.println("被代理的对象:" + getTargetResult);
System.out.println("代理对象自己:" + getThisResult);
System.out.println("被代理的对象:" + getTargetResult.getClass().getName());
System.out.println("代理对象自己:" + getThisResult.getClass().getName());
if(getTargetResult.equals(getThisResult)){
System.out.println("两者间的值相同");
}else {
System.out.println("两者间的值不相同");
}
if(getTargetResult == getThisResult){
System.out.println("两者间的内存地址相同");
}else {
System.out.println("两者间的内存地址不相同");
}
}
}
/***********************************执行结果********************************************/
被代理的对象:com.example.worktest.aop.controller.AopController@7cdc4070
代理对象自己:com.example.worktest.aop.controller.AopController@7cdc4070
被代理对象的类名:com.example.worktest.aop.controller.AopController
代理对象的类名:com.example.worktest.aop.controller.AopController$$EnhancerBySpringCGLIB$$877afd82
两者间的值不相同
两者间的内存地址不相同
从结果中可以看出target和this的内存地址不同,但是在第一个输出两个对象的时候却是相同的,这是为什么呢?
答:所谓代理类其实就是内部调用了目标类(被代理类)的方法,两个不同的对象内存地址不可能一样,上面的目标类和代理类打印调用的是toString(),但是代理类调用的其实是目标类的toString,所以地址一样,但是你如果用==比较是不一样的,也就是false。
结论
getTarget()该方法返回被织入增强处理的目标对象,这个和getthis()方法很相似 但是的出来的值、内存地址都是不相等的
ProceedingJoinPoint对象
ProceedingJoinPoint对象是JoinPoint的子接口,该对象只用在@Around的切面方法中,
添加了
Object proceed() throws Throwable //执行目标方法
Object proceed(Object[] var1) throws Throwable //传入的新的参数去执行目标方法
两个方法.