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 ...
随机推荐
- oracle 基础 执行sql文件
Oracle执行外部文件: c:>sqlplus user/pwd@db sql>@new.sql 执行多个sql文件: 1.把所有的文件都放在同一个目录下,然后在命令行里执行命令: ...
- 《Android应用性能优化》 第4章 高效使用内存
本地类型 大小 字节 boolean jboolean 8位(取决于VM) 1 byte jbyte 8位 1 char jchar 16位 2 short jshort 16位 2 int ji ...
- C语言基础问题总结
Double 类型用 %f 接受会产生错误,应该用%lf C语言中SIN,COS的参数为弧度,角度应先转换为弧度才能计算,否则出错 三角形的判断条件,任意两边长度和大于第三边 闰年判断条件整百年为40 ...
- div+css学习笔记一(转)
div+css学习笔记一 (2011-05-12 07:32:08) 标签: div css 居中 背景图片 ie6 ie7 margin 杂谈 分类: 网页制作 1.IE6中用了float之后导致m ...
- Mybatis的批处理以及执行Update返回行数为负数
项目中用到了批量更新. 在开发当中,可能经常会遇到批量处理这种情况,一般都再在java层面进行, 其本质是节省数据库连接打开关闭的的次数,占用更少的运行内存. 下面先记一下批处理映射吧: mybati ...
- JavaWeb 没用
Servlet的生命周期 初始化:Web容器加载servlet,调用innit(),只执行一次 处理业务: 请求到达时,运行service方法 并调用相应的doget或者dopost方法. 可执行多 ...
- Luogu 2939 [USACO09FEB]改造路Revamping Trails && Luogu 4568 [JLOI2011]飞行路线
双倍经验 写这两题之前被大佬剧透了呜呜呜. 分层图+最短路. 因为有$k$次机会能够把路径的费用变为$0$,我们可以建$k + 1$层图,对于每一层图我们把原来的边权和双向边连到上面去,而对于层与层之 ...
- Hive安装及配置
第一步:下载hive并解压 tar zxvf hive-0.8.1-bin.tar.gz 重命名: mv hive-0.8.1-bin hive 给权限:chown hadoop:hadoop hiv ...
- LeetCode第114题:二叉树展开为链表
问题描述 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解题思路 二叉树的一些算法题都可 ...
- Linux下c语言环境概述
Linux下C语言环境概述 主要涉及编辑器.编译链接器.调试器.项目管理工具 编辑器 Linux中常用的编辑器有vi和emacs 查看vim配置文件并编辑 编译链接器 在Linux中,最常用的编译器是 ...