SpringAOP里JoinPoint常用方法总结

@Before("customerJoinPointerExpression()")
public void beforeMethod(JoinPoint joinPoint){
	joinPoint.getSignature().getName(); // 获取目标方法名
	joinPoint.getSignature().getDeclaringType().getSimpleName(); // 获取目标方法所属类的简单类名
	joinPoint.getSignature().getDeclaringTypeName(); // 获取目标方法所属类的类名
	joinPoint.getSignature().getModifiers(); // 获取目标方法声明类型(public、private、protected)
	Object[] args = joinPoint.getArgs(); // 获取传入目标方法的参数,返回一个数组
	joinPoint.getTarget(); // 获取被代理的对象
	joinPoint.getThis(); // 获取代理对象自己
}

// 获取目标方法上的注解
private <T extends Annotation> T getMethodAnnotation(ProceedingJoinPoint joinPoint, Class<T> clazz) {
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    Method method = methodSignature.getMethod();
    return method.getAnnotation(clazz);
}

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP常用方法有以下几种: 1. 使用注解方式实现AOP:通过在目标方法上添加注解,然后在配置文件中配置切面和通知,实现对目标方法的增强。 ```java @Aspect @Component public class MyAspect { @Before("execution(* com.example.service.UserService.addUser(..))") public void beforeAddUser(JoinPoint joinPoint) { // 在目标方法执行之前执行的逻辑 System.out.println("Before adding user..."); } } ``` 2. 使用XML配置方式实现AOP:通过在配置文件中配置切面和通知,实现对目标方法的增强。 ```xml <bean id="myAspect" class="com.example.aspect.MyAspect" /> <aop:config> <aop:aspect ref="myAspect"> <aop:before method="beforeAddUser" pointcut="execution(* com.example.service.UserService.addUser(..))" /> </aop:aspect> </aop:config> ``` 3. 使用@Around注解实现环绕通知:通过在切面方法上添加@Around注解,可以在目标方法执行前后执行自定义逻辑。 ```java @Aspect @Component public class MyAspect { @Around("execution(* com.example.service.UserService.addUser(..))") public Object aroundAddUser(ProceedingJoinPoint joinPoint) throws Throwable { // 在目标方法执行之前执行的逻辑 System.out.println("Before adding user..."); // 执行目标方法 Object result = joinPoint.proceed(); // 在目标方法执行之后执行的逻辑 System.out.println("After adding user..."); return result; } } ``` 4. 使用@AfterReturning注解实现后置通知:通过在切面方法上添加@AfterReturning注解,可以在目标方法执行后执行自定义逻辑。 ```java @Aspect @Component public class MyAspect { @AfterReturning(pointcut = "execution(* com.example.service.UserService.addUser(..))", returning = "result") public void afterReturningAddUser(JoinPoint joinPoint, Object result) { // 在目标方法执行后执行的逻辑 System.out.println("After adding user..."); } } ``` 5. 使用@AfterThrowing注解实现异常通知:通过在切面方法上添加@AfterThrowing注解,可以在目标方法抛出异常时执行自定义逻辑。 ```java @Aspect @Component public class MyAspect { @AfterThrowing(pointcut = "execution(* com.example.service.UserService.addUser(..))", throwing = "ex") public void afterThrowingAddUser(JoinPoint joinPoint, Exception ex) { // 在目标方法抛出异常时执行的逻辑 System.out.println("Exception occurred while adding user: " + ex.getMessage()); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值