切面 Aspect ProceedingJoinPoint 获取当前类、方法等属性API

1 篇文章 0 订阅

切面配置类,获取切面上的各种属性:

示例

@Aspect
@Component
public class ProceedingJoinPointDemo {

 

   @Pointcut("execution(public * com.demo.controller.*.*.*(..))")
   public void allControllerMethod() {

   }

   @SuppressWarnings("rawtypes")
   @Around("allControllerMethod()")
   public Object aroundMethod(ProceedingJoinPoint point) throws Throwable {
   

        //拦截的实体类
        Object target = point.getTarget();
        //拦截的方法名称
        String methodName = point.getSignature().getName();
        //拦截的方法参数
        Object[] args = point.getArgs();
        //拦截的放参数类型
        Class[] parameterTypes = ((MethodSignature)point.getSignature()).getMethod().getParameterTypes();
        Method m = null;
        try {
        //通过反射获得拦截的method
        m = target.getClass().getMethod(methodName, parameterTypes);
        //如果是桥则要获得实际拦截的method
        if(m.isBridge()){
        for(int i = 0; i < args.length; i++){
        //获得泛型类型
        Class genClazz = GenericsUtils.getSuperClassGenricType(target.getClass());
        //根据实际参数类型替换parameterType中的类型
        if(args[i].getClass().isAssignableFrom(genClazz)){
        parameterTypes[i] = genClazz;
        }
        }
        //获得parameterType参数类型的方法
        m = target.getClass().getMethod(methodName, parameterTypes);
        }
        
        //放参数
        Integer userId = shiroSessionService.getUserId();
        Integer ownerCode = shiroSessionService.getOwnerCode();
        String userName = shiroSessionService.getUserName();
        Integer userType = shiroSessionService.getRoleId();
        String partnerId = shiroSessionService.getPartnerId();

        AdminUserVo userVo = new AdminUserVo();
        userVo.setUserId(userId);
        userVo.setOwnerCode(ownerCode);
        userVo.setAdminUsername(userName);
        userVo.setUserType(userType);
        userVo.setPartnerId(partnerId);
        args[1] = userVo;
        Object proceed = point.proceed(args);
        
        } catch (SecurityException e) {
        e.printStackTrace();
        } catch (NoSuchMethodException e) {
        e.printStackTrace();
        }
        return proceed;
    }

}

参考博文:https://www.cnblogs.com/chaoba/p/10503815.html

  • 10
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值