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;
} }
aspectj 与 spring 自定义注解的更多相关文章
- spring自定义注解实现登陆拦截器
1.spring自定义注解实现登陆拦截器 原理:定义一个注解和一个拦截器,拦截器拦截所有方法请求,判断该方法有没有该注解.没有,放行:有,要进行验证.从而实现方法加注解就需要验证是否登陆. 2.自定义 ...
- Spring 自定义注解,配置简单日志注解
java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致. 下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念. ...
- 2018-02-11 发布 spring 自定义注解(annotation)与 aop获取注解
知识点: Java自定义注解.spring aop @aspect的使用 首先我们先介绍Java自定义注解. 在开发过程中,我们实现接口的时候,会出现@Override,有时还会提示写@Suppres ...
- 【转】spring 自定义注解(annotation)与 aop获取注解
首先我们先介绍Java自定义注解. 在开发过程中,我们实现接口的时候,会出现@Override,有时还会提示写@SuppressWarnings.其实这个就是Java特有的特性,注解. 注解就是某种注 ...
- Spring 自定义注解,结合AOP,配置简单日志注解 (转)
java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致. 下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念. ...
- spring - 自定义注解
本自定义注解的作用:用于控制类方法的调用,只有拥有某个角色时才能调用. java内置注解 1.@Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括: ElemenetTy ...
- 照虎画猫写自己的Spring——自定义注解
Fairy已经实现的功能 读取XML格式配置文件,解析得到Bean 读取JSON格式配置文件,解析得到Bean 基于XML配置的依赖注入 所以,理所当然,今天该实现基于注解的依赖注入了. 基于XML配 ...
- 使用Spring自定义注解实现任务路由的方法
在Spring mvc的开发中,我们可以通过RequestMapping来配,当前方法用于处理哪一个URL的请求.同样我们现在有一个需求,有一个任务调度器,可以按照不同的任务类型路由到不同的任务执行器 ...
- Spring自定义注解扫描的实现
目标:实现自定义spring自动扫描注解.主要为后期实现分布式服务框架自动注解提供技术支持 技术分析:通过配置组件扫描标签使spring解析标签. 1. JewelScanBeanDefaultPar ...
随机推荐
- python dict.fromkeys()研究
def unique(seq): #return [x for x in my_list if x not in locals()['_[1]']] return {}.fromkeys(seq).k ...
- springmvc 注解式开发 处理器方法的返回值
1.返回void -Ajax请求 后台: 前台: 返回object中的数值型: 返回object中的字符串型: 返回object中的自定义类型对象: 返回object中的list: 返回object中 ...
- bzoj1951 组合数取模 中国剩余定理
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int a[4]={2,3,4679,35 ...
- java Iterator类
查看java源码. /* * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE ...
- 远程桌面--------ms12-020 漏洞复现 (死亡蓝屏)
漏洞名:MS12-020(全称:Microsoft windows远程桌面协议RDP远程代码执行漏洞) 介绍:RDP协议是一个多通道的协议,让用户连上提供微软终端机服务的电脑. windows在处理某 ...
- Android将程序崩溃信息保存本地文件
大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了 ...
- 回溯法和DFS leetcode Combination Sum
代码: 个人浅薄的认为DFS就是回溯法中的一种,一般想到用DFS我们脑中一般都有一颗解法树,然后去按照深度优先搜索去寻找解.而分支界限法则不算是回溯,无论其是采用队列形式的还是优先队列形式的分支界限法 ...
- sqlserver小批量导数据
USE [KM_Voice] GO /****** Object: StoredProcedure [dbo].[proc_insert] Script Date: 01/09/2015 18: ...
- await 与 SynchronizationContext 关系
static async Task DoStep() { //step 1 Debug.WriteLine("DoStep Start thread id: " + System. ...
- 工作中用的cobbler命令行
在使用cobbler服务器,从pxe启动虚机的时候,经常用到的cobbler命令行 1.查看注册信息 cobbler system report --name=test25 2.注册信息 cobble ...