**
* 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;
} }

aspectj 与 spring 自定义注解的更多相关文章

  1. spring自定义注解实现登陆拦截器

    1.spring自定义注解实现登陆拦截器 原理:定义一个注解和一个拦截器,拦截器拦截所有方法请求,判断该方法有没有该注解.没有,放行:有,要进行验证.从而实现方法加注解就需要验证是否登陆. 2.自定义 ...

  2. Spring 自定义注解,配置简单日志注解

    java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致. 下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念. ...

  3. 2018-02-11 发布 spring 自定义注解(annotation)与 aop获取注解

    知识点: Java自定义注解.spring aop @aspect的使用 首先我们先介绍Java自定义注解. 在开发过程中,我们实现接口的时候,会出现@Override,有时还会提示写@Suppres ...

  4. 【转】spring 自定义注解(annotation)与 aop获取注解

    首先我们先介绍Java自定义注解. 在开发过程中,我们实现接口的时候,会出现@Override,有时还会提示写@SuppressWarnings.其实这个就是Java特有的特性,注解. 注解就是某种注 ...

  5. Spring 自定义注解,结合AOP,配置简单日志注解 (转)

    java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致. 下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念. ...

  6. spring - 自定义注解

    本自定义注解的作用:用于控制类方法的调用,只有拥有某个角色时才能调用. java内置注解 1.@Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括: ElemenetTy ...

  7. 照虎画猫写自己的Spring——自定义注解

    Fairy已经实现的功能 读取XML格式配置文件,解析得到Bean 读取JSON格式配置文件,解析得到Bean 基于XML配置的依赖注入 所以,理所当然,今天该实现基于注解的依赖注入了. 基于XML配 ...

  8. 使用Spring自定义注解实现任务路由的方法

    在Spring mvc的开发中,我们可以通过RequestMapping来配,当前方法用于处理哪一个URL的请求.同样我们现在有一个需求,有一个任务调度器,可以按照不同的任务类型路由到不同的任务执行器 ...

  9. Spring自定义注解扫描的实现

    目标:实现自定义spring自动扫描注解.主要为后期实现分布式服务框架自动注解提供技术支持 技术分析:通过配置组件扫描标签使spring解析标签. 1. JewelScanBeanDefaultPar ...

随机推荐

  1. DAY7-面向对象之绑定方法与非绑定方法

    一.类中定义的函数分成两大类 一:绑定方法(绑定给谁,谁来调用就自动将它本身当作第一个参数传入): 1. 绑定到类的方法:用classmethod装饰器装饰的方法. 为类量身定制 类.boud_met ...

  2. 关于EF中实体和数据表以及查询语句映射的问题

    关于EF中实体和数据表以及查询语句映射的问题? 很多人在使用的时候分不清楚 实体字段应该少于等于(数据库中的表字段或者SQL查询中的临时字段).这样在查询或者添加修改都不会出现问题 如果实体的字段大于 ...

  3. css垂直居中方法(四)

    第六种方法,使用css的writing-mode属性,结合margin:auto. 参考文章:改变CSS世界纵横规则的writing-mode属性 传统的web流中,margin设置auto值的时候, ...

  4. Android 数据库 OrmLite Failed to open database

    04-01 16:49:32.720: E/SQLiteLog(1894): (14) cannot open file at line 30204 of [00bb9c9ce4]04-01 16:4 ...

  5. CSS中cursor的pointer 与 hand(转)

    CSS中cursor的pointer 与 hand 转载 2015年12月25日 16:18:36 标签: cursorpointer / cursorhand 1781 cursor:hand 与 ...

  6. ???SpringMVC_03 利用SpringMVC提供的过滤器解决浏览器请求参数的乱码问题

    1 响应乱码问题 在启用mvc注解的配置中添加一个转换器配置 <?xml version="1.0" encoding="UTF-8"?> < ...

  7. MSScriptControl详解(可实现在C#等语言中调用JAVASCRIPT代码)

    ScriptControl接口 属性名称 类型 备注 AllowUI BOOL 检测是否允许运行用户的接口元素.如果为False,则诸如消息框之类的界面元素不可见. CodeObject Object ...

  8. 算法Sedgewick第四版-第1章基础-002一些工具类算法(Euclid’s algorithm)

    1. //Euclid’s algorithm public static int gcd(int p, int q) { if (q == 0) return p; int r = p % q; r ...

  9. spring 中配置sessionFactory及用法

    spring 中配置sessionFactory及用法 方法一: 1.在Spring的applicationContext.xml中配置bean <!-- 启用注解注入  -->      ...

  10. 数据结构_find_lucky_number(寻找幸运值)

    数据结构_find_lucky_number(寻找幸运值) 问题描述 给出两个已按升序排列的数组 a[1..n],b[1..m],如果存在 i,j,使得a[i]+b[j]==k,我们便说已找到幸运值. ...