自定义注解 配置缓存

/***************************************************************************************************************/

@Pointcut("@annotation(com.cache.com_cache.framework.annotation.LocalCache)")
    public void localCachePointcut() {}

   
     /* 环绕通知第一个参数必须是ProceedingJoinPoint ProceedingJoinPoint proceed()是执行改方法 里面可以传入Object[] 是表示改方法执行时传入的参数
     */
    @Around(value = "localCachePointcut()")
    public Object get(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();// 获取参数列表
        Object proceed = null;
        Method method = null;
        try {
            method = getMethod(joinPoint);
        } catch (Exception e) {
            logger.error("获取方法异常", e);
        }
        logger.info("获取的方法对象:" + method);

        String key = null;//定义缓存上的key值
        // 获取注解上的参数
        // LocalCache localCache = getAnnotation(joinPoint);
        LocalCache localCache = method.getAnnotation(LocalCache.class);// 获取注解上的参数
        if (localCache != null) {
            key = localCache.key();
        }
        logger.info("获取的key值:" + key);
        if (args != null && args.length > 0) {
            proceed = joinPoint.proceed(args);
        } else {
            proceed = joinPoint.proceed();
        }
        LocalCacheUtil.getInstance().setLocalCache(key, proceed);
        return proceed;
    }

    /**
     * 获取当前执行的方法
     */
    public Method getMethod(ProceedingJoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        return method;
    }

    /**
     * 获取当前注解
     */
    @SuppressWarnings("unused")
    private LocalCache getAnnotation(final ProceedingJoinPoint pJointPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) pJointPoint.getSignature();
        final Object target = pJointPoint.getTarget();
        Method method = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
        return method.getAnnotation(LocalCache.class);
    }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值