**
* 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. JavaSwing文件选择器 JFileChooser的使用

    先看效果吧! 说明:选择文件或者文件夹.本例子就直接在控制台输出文件或者文件夹的路径.实际开发中,就可以将文件或文件夹的路径封装为File的实例来使用了. package test; import j ...

  2. css水平居中(一)

    第一种方法:使用text-align属性. 看到一篇博客,也不知道是不是我理解的问题,博客上说text-align可以是内联元素水平居中,我感觉这样的说法是不是有些不准确. text-align属性规 ...

  3. 数据从HDFS-->HIVE-->HBASE 执行过程

    1.数据已经load进去hdfs 2.hive.hbase已经安装成功(我用的是hadoop 2.4 hbase 0.98.12 hive 1.2.1) 3.开始! 4.在hive建立表同时生成对应的 ...

  4. Eclipse中,将tab缩进改为4个空格

    用4个空格来缩进 , 不要用Tab来缩进 , 因为Tab在不同平台的点位不一样 eclipse->preferences->General->Editors->Text Edi ...

  5. C语言获取系统时间

    localtime函数 #include <stdio.h> #include <time.h> int main () { time_t t; struct tm *lt; ...

  6. this、new、call和apply的相关问题

    讲解this指针的原理是个很复杂的问题,如果我们从javascript里this的实现机制来说明this,很多朋友可能会越来越糊涂,因此本篇打算换一个思路从应用的角度来讲解this指针,从这个角度理解 ...

  7. 在JAVA中,String,Stringbuffer,StringBuilder 的区别

    首先是,String,StringBuffer的区别 两者的主要却别有两方面,第一是线程安全方面,第二是效率方面 线程安全方面: String  不是线程安全的,这意味着在不同线程共享一个String ...

  8. ubuntu16安装pylearn2 出现错误提示importerror:no module named six.moves

    由于市面上的一些教程时间比较早,入门学习时跟随教程安装容易出现各种错误,这些错误基本都是版本不同导致的 所以,我们安装过程中一定要指出包的版本,如果你已经遇到no module named six.m ...

  9. Entity Framework Tutorial Basics(18):DBEntityEntry Class

    DBEntityEntry Class DBEntityEntry is an important class, which is useful in retrieving various infor ...

  10. review backpropagation

    The goal of backpropagation is to compute the partial derivatives ∂C/∂w and ∂C/∂b of the cost functi ...