aspectj 与 spring 自定义注解

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

}
原文地址:https://www.cnblogs.com/whm-blog/p/7156469.html