package com.aop;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import com.core.message.ReturnMessage;
@Aspect
@Component
public class ExceptionAop {
private void printTime() {
Date time = new Date();
SimpleDateFormat sdFormat = new SimpleDateFormat(
"yyyyMMdd hh:mm:ss:SSS");
String myTime = sdFormat.format(time);
System.out.println("-----------" + myTime);
}
@Around(value = "execution(* com.action..*.*(..))")
public Object doExceptionAop(ProceedingJoinPoint jp) {
System.out.println("----------安全处理之前-------------");
Object obj = null;
try {
this.printTime();
obj = jp.proceed();
System.out.println(obj.toString());
this.printTime();
} catch (Throwable e) {
e.printStackTrace();
return ReturnMessage.returnError();
}
System.out.println("----------安全处理之后-------------");
return obj;
}
}
使用spring aop 技术,拦截action层所有方法,方法产生异常,截获异常,返回统一的错误信息。