**
* ErrorCode:
*
* @author yangzhenlong
* @since 2016/7/21
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface ErrorException {
int code() default 0;//参数
}
复制代码
Aspect拦截注解类 复制代码
/**
* ErrorExceptionAspect:
*
* @author yangzhenlong
* @since 2016/7/21
*/
@Component
@Aspect
public class ErrorExceptionAspect { //@Before("execution(* com.sarkuya.service..*.*(..))")
@Pointcut(value = "@annotation(com.mlxs.mvc.anno.ErrorException)")
private void pointcut() {
} @Around(value = "pointcut() && @annotation(errorExecption)")
public Object around(ProceedingJoinPoint point, ErrorException errorExecption){
System.out.println("---->around");
//注解参数
System.out.println("注解参数:"+ errorExecption.code());
//当前拦截的类和方法:
Class clazz = point.getTarget().getClass();
Method method = ((MethodSignature) point.getSignature()).getMethod(); String codeName = clazz.getSimpleName()+"_"+method.getName();
System.out.println("query param---->"+codeName); //方法返回结果
Object result = null;
Object args = Arrays.asList(point.getArgs());
try {
//执行方法(可以在方法前后添加前置和后置通知)
result = point.proceed();
//校验结果
result = validateResult(result);
} catch (Throwable e) {
//记录日志
System.out.println(codeName + "()方法异常:" + e);
//打印堆栈信息
e.printStackTrace();
//设置返回信息
result = "结果:抛了异常了。。-----------------------"+e.getMessage()+",原因:"+e.getCause();
}
//返回通知
return result; } /**
* 方法执行后
* @param joinPoint
* @param result
*/
@AfterReturning(value = "pointcut() && @annotation(errorExecption)", returning = "result")
public Object afterReturning(JoinPoint joinPoint, ErrorException errorExecption, Object result){
System.out.println("---->afterReturning");
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " return with " + result);
if(result instanceof Boolean){
if(!((Boolean) result)){
result = "error----result is false";
}
}else{
if(result == null){
result = "error----result is null";
}
}
return result;
}
/**
* 方法执行后
* @param joinPoint
* @param ex
*/
@AfterThrowing(value = "pointcut() && @annotation(errorExecption)", throwing = "ex")
public void afterThrowing(JoinPoint joinPoint, ErrorException errorExecption, Exception ex){
System.out.println("eeeee--->afterThrowing");
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + "occurs exception: " + ex);
} private Object validateResult(Object result){
if(result instanceof Boolean){
if(!((Boolean) result)){
System.out.println("error----result is false");
result = "error:false";
}
}else{
if(result == null){
System.out.println("error----result is null");
result = "error:null";
}
}
return result;
}
}
复制代码
测试: 复制代码
/**
* _Test:
*
* @author yangzhenlong
* @since 2016/7/21
*/
@Component("test")
public class _Test { public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("classpath*:spring/applicationContext.xml");
_Test obj = (_Test) context.getBean("test");
System.out.println("==========>"+obj.test());
//System.out.println("==========>"+obj.test2());
} @ErrorException(code = 100)
public Object test(){
System.out.println("---test---");
int a = 10/0;
return 20;
} @ErrorException(code = 22)
public Object test2(){
System.out.println("---test2---");
//int a = 10/0;
return false;
} }

最新文章

  1. 新版Retrofit 2可运行例子(解决Could not locate ResponseBody converter for问题)
  2. iOS开发-完整学习路线图
  3. Android permission
  4. Android消息处理机制
  5. 威胁远胜“心脏出血”?国外新爆Bash高危安全漏洞
  6. BZOJ 1061: [Noi2008]志愿者招募 费用流
  7. VPS用LNMP安装WordPress
  8. FpSpread添加标注
  9. java初学者必看经典
  10. H264中的MapUnits
  11. 读取cc2530节点的设备类型、协调器、路由器、终端。
  12. IOS 消息
  13. this的指向问题
  14. 201621123050 《Java程序设计》第4周学习总结
  15. [LeetCode] Maximum Distance in Arrays 数组中的最大距离
  16. Win32文件系统编程
  17. oracle move 释放 表空间
  18. Ng第六课:逻辑回归(Logistic Regression)
  19. Java 多线程学习笔记:wait、notify、notifyAll的阻塞和恢复
  20. Simple Web API Server in Golang (1)

热门文章

  1. sh 脚本重启/更新 Tomcat 项目
  2. uniqid() 函数 和 microtime()函数
  3. DR客户端一直连接服务器....(6)
  4. hive与hbase数据类型对应关系
  5. loj2395 [JOISC 2017 Day 2]火车旅行
  6. svn跨多个版本比较
  7. vs2010 怎样设置文本编辑窗口和解决方案资源管理器同步?
  8. HttpRunnerManager接口自动化测试框架测试报告页面优化
  9. c++内联 inline
  10. 51nod - 1179 - 最大的最大公约数 - 枚举