- 获取切入点方法的名字
getSignature());是获取到这样的信息 :修饰符+ 包名+组件名(类名) +方法名,
这里我只需要方法名
String methodName = joinPoint.getSignature().getName()
- 获取方法上的注解
- 方法1:xxxxxx是注解名字
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null)
{
xxxxxx annoObj= method.getAnnotation(xxxxxx.class);
}
return null;
- 方法2:上面我们已经知道了方法名和类的对象,通过反射可以获取类的内部任何信息。
// 切面所在类
Object target = joinPoint.getTarget();
String methodName = joinPoint.getSignature().getName();
Method method = null;
for (Method m : target.getClass().getMethods()) {
if (m.getName().equals(methodName)) {
method = m;
// xxxxxx annoObj= method.getAnnotation(xxxxxx.class);同上
break;
}
}
- 获取方法的参数
这里返回的是切入点方法的参数列表
Object[] args = joinPoint.getArgs();
测试
眼见为实,测试一遍可以理解更深刻
@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ApiLog
{
/**
* 模块
*/
public String title() default "";
/**
* 日志记录service实现
* @return
*/
public String logService() default "operLogServiceImpl";
/**
* 是否保存请求的参数
*/
public boolean isSaveRequestData() default true;
/**
* 是否追踪用户操作
* @return
*/
public boolean isTrack() default true;
}
切面类
package com.kouryoushine.aop.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@Aspect
@Component
public class DemoAspect {
//切入点:aopdemo报下所有对象的save方法
@Pointcut("execution(public * com.kouryoushine.aop.test.*.save*(..))")
public void save(){
}
/**
* 需要在update操作前后分别获取更新前后的值
* @param
* @return
*/
@AfterReturning("save()")
public void afterReturn(JoinPoint joinPoint) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
//1.获取切入点所在目标对象
Object targetObj =joinPoint.getTarget();
System.out.println(targetObj.getClass().getName());
// 2.获取切入点方法的名字
String methodName = joinPoint.getSignature().getName();
System.out.println("切入方法名字:"+methodName);
// 3. 获取方法上的注解
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null)
{
ApiLog apiLog= method.getAnnotation(ApiLog.class);
System.out.println("切入方法注解的title:"+apiLog.title());
}
//4. 获取方法的参数
Object[] args = joinPoint.getArgs();
for(Object o :args){
System.out.println("切入方法的参数:"+o);
}
}
}
服务类
@Service
public class TestServcie {
@ApiLog(title = "注解的标题",isSaveRequestData = false)
public void save(String parm1,int parm2){
System.out.println("执行目标对象的方法"+parm1+parm2);
}
public void update(){
System.out.println("没有注解的方法,不会被拦截");
}
}
测试方法
@Autowired
TestServcie testServcie;
@Test
void test6() throws Exception{
testServcie.save("参数1字符串",33);
}
测试结果
com.kouryoushine.aop.test.TestServcie
切入方法名字:save
切入方法注解的title:注解的标题
切入方法的参数:参数1字符串
切入方法的参数:33