spring 切面编程中获取具体方法的方法

  工作中,使用环绕通知,用来捕获异常,然后通过获取方法的返回值,返回不同的数据给到调用方。

  由于方法的返回值不同,我们处理异常时,也需要返回不同的格式。

  这时,就需要获取具体方法了的返回值了。

  我的环绕通知方法如下:

@Around(value = "GlobalControllerAspect.controllerPointcut()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object result = null;
try {
long statrTime=System.currentTimeMillis();
result = proceedingJoinPoint.proceed();
long endTime=System.currentTimeMillis();
String declaringTypeName = proceedingJoinPoint.getSignature().getDeclaringTypeName();
String methodName = proceedingJoinPoint.getSignature().getName();
StringBuilder spendTimeLog=new StringBuilder("方法名称:").append(declaringTypeName).append(".").append(methodName).append("() 耗时为:").append(endTime-statrTime).append("ms.");
logger.info(spendTimeLog.toString());
} catch (Exception e) {
logger.error("环绕通知捕获异常信息:",e);
ResponseModel responseModel=new ResponseModel();
if (e instanceof RuntimeException) {
responseModel.setStatus(ResponseModel.STATUS_FAIL);
responseModel.setMsgType(ResponseModel.MSGTYPE_INFO);
responseModel.setMsgContent(e.getMessage());
} else {
responseModel.setMsgType(ResponseModel.MSGTYPE_WARINNING);
responseModel.setStatus(ResponseModel.STATUS_ERROR);
responseModel.setMsgContent(e.getMessage());
}
Signature sig = proceedingJoinPoint.getSignature();
MethodSignature msig = null;
if (!(sig instanceof MethodSignature)) {
throw new IllegalArgumentException("该注解只能用于方法");
}
msig = (MethodSignature) sig;
Object target = proceedingJoinPoint.getTarget();
Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
Class<?> returnType = currentMethod.getReturnType();
if(returnType.isAssignableFrom(responseModel.getClass())||returnType==ResponseModel.class){ return responseModel;
}else if(returnType==JSONObject.class){
return responseModel.toJSON();
}
}
return result;
}

获取具体方法的代码:

Signature sig = proceedingJoinPoint.getSignature();
MethodSignature msig = null;
if (!(sig instanceof MethodSignature)) {
throw new IllegalArgumentException("该注解只能用于方法");
}
msig = (MethodSignature) sig;
Object target = proceedingJoinPoint.getTarget();
Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
Class<?> returnType = currentMethod.getReturnType();

获取到具体方法,返回值就很容易获取了。

spring aop 切面编程中获取具体方法的方法的更多相关文章

  1. Spring AOP 切面编程记录日志和接口执行时间

    最近客户现在提出系统访问非常慢,需要优化提升访问速度,在排查了nginx.tomcat内存和服务器负载之后,判断是数据库查询速度慢,进一步排查发现是因为部分视图和表查询特别慢导致了整个系统的响应时间特 ...

  2. Java编程中获取键盘输入实现方法及注意事项

    Java编程中获取键盘输入实现方法及注意事项 1. 键盘输入一个数组 package com.wen201807.sort; import java.util.Scanner; public clas ...

  3. Spring AOP 切面编程的方法

    spring aop的使用分为两种,一种是使用注解来实现,一种是使用配置文件来实现. 先来简单的介绍一下这两种方法的实现,接下来详细的介绍各处的知识点便于查阅.目录如下: 1.基于注解实现spring ...

  4. Spring AOP 切面编程实战Demo项目

    为什么会有此项目?在某日,我看博客时,看到了讲面向切面编程的内容,之前也知道spring是面向切面编程的,只是自己没有写过相关的代码,于是决定自己写一个test.但是url拦截器从外部看,和AOP有相 ...

  5. spring aop切面编程实现操作日志步骤

    1.在spring-mvc.xml配置文件中打开切面开关: <aop:aspectj-autoproxy proxy-target-class="true"/> 注意: ...

  6. spring aop 切面编程

    import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Ha ...

  7. 快速高效掌握企业级项目中的Spring面向切面编程应用,外带讲面试技巧

    Spring面向切面编程(AOP)是企业级应用的基石,可以这样说,如果大家要升级到高级程序员,这部分的知识必不可少. 这里我们将结合一些具体的案例来讲述这部分的知识,并且还将给出AOP部分的一些常见面 ...

  8. Spring MVC通过AOP切面编程 来拦截controller 实现日志的写入

    首选需要参考的是:[参考]http://www.cnblogs.com/guokai870510826/p/5977948.html    http://www.cnblogs.com/guokai8 ...

  9. Spring 面向切面编程(AOP)

    Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...

随机推荐

  1. JavaScript Array some() 方法

    some 判断数组中是否至少有一个元素满足条件 只要有一个满足就返回true 只有都不满足时才返回false 语法: array.some(function(value,index,array),th ...

  2. 从servlet规范说起

    servlet规范 1 servlet 3.1规范 1.1 What is servlet A servlet is a JavaTM technology-based Web component, ...

  3. jquery中的 parseJSON() 源码分析

    parseJSON: function( data ) { // Attempt to parse using the native JSON parser first if ( window.JSO ...

  4. 面试必备:ArrayList源码解析(JDK8)

    面试必备:ArrayList源码解析(JDK8) https://blog.csdn.net/zxt0601/article/details/77281231 概述很久没有写博客了,准确的说17年以来 ...

  5. python的车牌号的检测

    自己总结一下,从网上找到的关于车牌号的识别的一些博文.https://www.jianshu.com/p/fcfbd3131b84 https://www.cnblogs.com/do-hardwor ...

  6. 阿里云 部署dashboard

    本文是基于kubeasz 进行部署安装,将部署心得记录下来.可以查看原文地址: https://github.com/gjmzj/kubeasz/blob/master/docs/guide/dash ...

  7. 一.C#基础:标识符和关键字

    只要语言就会有标识符,和关键字的概念,以下是C#的标识符与关键字: 一个字符串要成为标识符要满足的条件1:只包含字母,(包括大小写),数字,@符号,下划线——.2:首位可以是字母,@,但一定不能是数字 ...

  8. 动态在线扩容root根分区大小的方法详解

    前言 本文主要介绍了关于动态在线扩容root根分区大小的相关内容,分享出来供大家参考学习,下面话不都说了,来一起看看详细的介绍吧. ? 1 qemu-img resize yourname.img + ...

  9. 【centos】/usr/bin与/usr/local/bin的区别

    首先注意usr 指 Unix System Resource,而不是User 然后通常: /usr/bin下面的都是系统预装的可执行程序,会随着系统升级而改变. /usr/local/bin目录是给用 ...

  10. [Draft]iOS.Architecture.16.Truth-information-flow-and-clear-responsibilities-immutability

    Concept: Truth, Information Flow, Clear Responsibilities and Immutability 1. Truth 1.1 Single Source ...