重构方法
This commit is contained in:
@@ -36,10 +36,6 @@ abstract class AppAspect {
|
||||
*/
|
||||
@Around("func()")
|
||||
fun around(point: ProceedingJoinPoint): Any? {
|
||||
return try {
|
||||
point.proceed()
|
||||
} catch (ex: Throwable) {
|
||||
val clazz = point.`this`.javaClass //获取实际对象的类型避免获取到父类
|
||||
val func = point.signature.declaringType.methods.find {
|
||||
it.name == point.signature.name
|
||||
}!!//获取声明类型中的方法信息
|
||||
@@ -48,24 +44,22 @@ abstract class AppAspect {
|
||||
var funcName = func.name
|
||||
//遍历方法注解
|
||||
for (funcAnnotation in funcAnnotations) {
|
||||
val annotations = funcAnnotation.annotationClass.annotations
|
||||
if (funcAnnotation is Method)
|
||||
funcName = funcAnnotation.name
|
||||
|
||||
val annotations = funcAnnotation.annotationClass.annotations
|
||||
//尝试寻找方法注解的处理类
|
||||
val handler = annotations.find { it is Handler }
|
||||
if (handler != null && handler is Handler) {
|
||||
val handleClazz = applicationContext.getBean(handler.value.java)
|
||||
handleClazz.handle(clazz, func, point.args)
|
||||
handleClazz.handle(point.`this`, func, point.args)
|
||||
}
|
||||
if (funcAnnotation is Method)
|
||||
funcName = funcAnnotation.name
|
||||
}
|
||||
|
||||
//找到类的模块名称,否则使用类名
|
||||
var moduleName = clazz.simpleName
|
||||
val module = clazz.annotations.find { it is Module }
|
||||
if (module != null && module is Module) {
|
||||
moduleName = module.name
|
||||
}
|
||||
return try {
|
||||
point.proceed()
|
||||
} catch (ex: Throwable) {
|
||||
val moduleName = this.resolveModuleName(point.`this`)
|
||||
var message = "$moduleName - ${funcName}异常"
|
||||
message = if (ex is NoticeUserException || ex is Error) {
|
||||
"$message: ${ex.message}"
|
||||
@@ -83,4 +77,24 @@ abstract class AppAspect {
|
||||
return HttpMessage(Status.Error, message)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析模块名
|
||||
*/
|
||||
private fun resolveModuleName(obj: Any): String {
|
||||
val clazz = obj.javaClass
|
||||
// 1.默认使用类名作为模块名
|
||||
var moduleName = clazz.simpleName
|
||||
// 2.找到类的模块注解解析名称
|
||||
val module = clazz.annotations.find { it is Module }
|
||||
if (module != null && module is Module) {
|
||||
moduleName = module.name
|
||||
}
|
||||
// 3.尝试找类的name字段作为模块名称
|
||||
val nameField = clazz.fields.find { it.name == "name" }
|
||||
if (nameField != null) {
|
||||
moduleName = nameField.get(obj).toString()
|
||||
}
|
||||
return moduleName
|
||||
}
|
||||
}
|
||||
@@ -15,11 +15,11 @@ class AccessLogHandler : AnnotationHandler {
|
||||
@Autowired
|
||||
lateinit var logger: ILogger
|
||||
|
||||
override fun handle(clazz: Class<Any>, func: Method, args: Array<Any>, exception: Exception?) {
|
||||
override fun handle(obj: Any, func: Method, args: Array<Any>, exception: Exception?) {
|
||||
val attributes = RequestContextHolder.getRequestAttributes() as ServletRequestAttributes
|
||||
val request = attributes.request
|
||||
logger.info(
|
||||
"${request.method} ${request.requestURL} from ${request.remoteAddr}, call function ${clazz.name}.${func.name}, args: ${
|
||||
"${request.method} ${request.requestURL} from ${request.remoteAddr}, call function ${func.toGenericString()}, args: ${
|
||||
mapper.writeValueAsString(
|
||||
args
|
||||
)
|
||||
|
||||
@@ -2,6 +2,17 @@ package com.synebula.gaea.spring.aop.handler
|
||||
|
||||
import java.lang.reflect.Method
|
||||
|
||||
/**
|
||||
* 注解对应的方法处理对象接口
|
||||
*/
|
||||
interface AnnotationHandler {
|
||||
fun handle(clazz: Class<Any>, func: Method, args: Array<Any>, exception: Exception? = null)
|
||||
|
||||
/**
|
||||
* 处理“被注解方法”的方法
|
||||
* @param obj 处理的类对象
|
||||
* @param func 处理的方法
|
||||
* @param args 处理的方法参数信息
|
||||
* @param exception 处理的异常信息
|
||||
*/
|
||||
fun handle(obj: Any, func: Method, args: Array<Any>, exception: Exception? = null)
|
||||
}
|
||||
Reference in New Issue
Block a user