import com.meiliwan.emall.commons.jedisTool.JedisKey;
import java.lang.annotation.*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface HelloWorld {
public JedisKey key();
}
package com.meiliwan.emall.pms.util;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.meiliwan.emall.commons.jedisTool.JedisKey;
import com.meiliwan.emall.commons.jedisTool.ShardJedisTool;
import com.meiliwan.emall.commons.util.StringUtil;
import com.meiliwan.emall.icetool.JSONTool;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.lang.reflect.Method;
import static com.meiliwan.emall.icetool.JSONTool.addToResult;
@Aspect
@Component
public class HelloWorldAspect {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Pointcut("execution(* com.meiliwan.emall.*.service..*Service.*(..)) && @annotation(com.meiliwan.emall.pms.util.HelloWorld)")
public void helloWorldAnnotated() {}
@Before("helloWorldAnnotated()")
public void before(JoinPoint jp) throws Throwable{
logger.debug("==============================before: "
+ jp.getSignature());
}
@Around("helloWorldAnnotated()")
public void around(ProceedingJoinPoint pjp) throws Throwable{
logger.debug("==============================@Around.before");
//获取方法名称
String methodName = pjp.getSignature().getName();
if (StringUtils.isNotEmpty(methodName)){
//获取目标类名
Class targetClass = pjp.getTarget().getClass();
Method[] methods = targetClass.getDeclaredMethods();
if (methods != null){
Method mt = null;
//循环匹配对应的查找方法
for(Method method : methods){
if(method == null)continue;
if(method.getName().equals(methodName)){
mt = method;
break;
}
}
if (mt!=null){
boolean hasAnnotation = mt.isAnnotationPresent(HelloWorld.class);
if (hasAnnotation){
HelloWorld annotations = mt.getAnnotation(HelloWorld.class);
JedisKey key = annotations.key();
if (key!=null&&!key.equals("")){
//获取方法参数
Object[] args = pjp.getArgs();
JsonObject resultObj = (JsonObject)args[0];
Object redidId = args[1];
//加一个异常捕获,用于特殊情况,如果缓存宕机或者链接超时等
try {
//查缓存
String obj = ShardJedisTool.getVolatile().get(key, (Serializable) redidId);
if(obj!=null&&!obj.equals("")){
JsonObject redisObj = (JsonObject)(new JsonParser().parse(obj));
addToResult(redisObj.get("resultObj").getAsJsonObject(),resultObj);
} else{
//走数据库查询
pjp.proceed();
//缓存到缓存中
ShardJedisTool.getVolatile().set(key, (Serializable) redidId, resultObj);
}
}catch (Exception e){
logger.debug("========缓存宕机或者链接超时等=======");
pjp.proceed();
}
}
}
}
}
}
logger.debug("==============================@Around.after");
}
@After("helloWorldAnnotated()")
public void after(JoinPoint joinPoint) {
logger.debug("==============================after: "
+ joinPoint.getSignature());
}
}