/***************************************************************************************************************/
@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);
}