67,513
社区成员
发帖
与我相关
我的任务
分享
package com.hexin.pettyLoan.common.aop;
import java.util.Date;
import javax.annotation.Resource;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import com.hexin.core.annotation.AnnotationUtil;
import com.hexin.core.annotation.RedisRead;
import com.hexin.core.util.JSONUtil;
import com.hexin.core.util.cache.ShardedRedisUtil;
@Component
@Aspect
public class AspectJRedisRead {
@Pointcut("execution(* com.hexin.pettyLoan.*.service.impl.*.*(..))")
private void pointCut(){}
public AspectJRedisRead(){
System.out.println("aaaaaa");
}
// @Resource(name="shardedRedisUtil")
// ShardedRedisUtil redisUtil;
@Before("pointCut()") //spring中Before通知
public void readBefore() {
System.out.println("readBefore:现在时间是:"+new Date());
}
@After("pointCut()") //spring中After通知
public void readAfter() {
System.out.println("readAfter:现在时间是:"+new Date());
}
@AfterReturning("pointCut()")
public void readAfterReturning(){
System.out.println("readAfterReturning:现在时间是:"+new Date());
}
@AfterThrowing("pointCut()")
public void readAfterThrowing(){
System.out.println("readAfterThrowing:现在时间是:"+new Date());
}
@Around("pointCut()") //spring中Around通知
public Object readAround(ProceedingJoinPoint joinPoint) {
Object result = null;
try {
System.out.println("readAround开始:现在时间是:"+new Date());
result = joinPoint.proceed(joinPoint.getArgs());
System.out.println("readAround结束:现在时间是:"+new Date());
} catch (Throwable e) {
e.printStackTrace();
}
return result;
}
}
<context:annotation-config></context:annotation-config>
<!-- 启用aop -->
<aop:aspectj-autoproxy proxy-target-class="true" />
@Service("flexkeyService")
public class FlexkeyServiceImpl implements FlexkeyService {
@Override
public String redisTest(Integer id){
return "this is from function";
}
}
@RequestMapping("/redistest.do")
public @ResponseBody String redistest(String callback){
JsonResult result = new JsonResult();
try{
result = new JsonResult(1, null, flexkeyService.redisTest(1));
}
catch(ErrorCodeException ex){
result = new JsonResult(-1, ex.toMessage(), null);
logger.error(ex.toMessage(), ex);
}
String json =JSONUtil.toJsonpString(result, callback);
return json;
}