重构方法
This commit is contained in:
@@ -36,36 +36,30 @@ abstract class AppAspect {
|
|||||||
*/
|
*/
|
||||||
@Around("func()")
|
@Around("func()")
|
||||||
fun around(point: ProceedingJoinPoint): Any? {
|
fun around(point: ProceedingJoinPoint): Any? {
|
||||||
|
val func = point.signature.declaringType.methods.find {
|
||||||
|
it.name == point.signature.name
|
||||||
|
}!!//获取声明类型中的方法信息
|
||||||
|
val funcAnnotations = func.annotations ?: arrayOf()
|
||||||
|
|
||||||
|
var funcName = func.name
|
||||||
|
//遍历方法注解
|
||||||
|
for (funcAnnotation in funcAnnotations) {
|
||||||
|
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(point.`this`, func, point.args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return try {
|
return try {
|
||||||
point.proceed()
|
point.proceed()
|
||||||
} catch (ex: Throwable) {
|
} catch (ex: Throwable) {
|
||||||
val clazz = point.`this`.javaClass //获取实际对象的类型避免获取到父类
|
val moduleName = this.resolveModuleName(point.`this`)
|
||||||
val func = point.signature.declaringType.methods.find {
|
|
||||||
it.name == point.signature.name
|
|
||||||
}!!//获取声明类型中的方法信息
|
|
||||||
val funcAnnotations = func.annotations ?: arrayOf()
|
|
||||||
|
|
||||||
var funcName = func.name
|
|
||||||
//遍历方法注解
|
|
||||||
for (funcAnnotation in funcAnnotations) {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
var message = "$moduleName - ${funcName}异常"
|
var message = "$moduleName - ${funcName}异常"
|
||||||
message = if (ex is NoticeUserException || ex is Error) {
|
message = if (ex is NoticeUserException || ex is Error) {
|
||||||
"$message: ${ex.message}"
|
"$message: ${ex.message}"
|
||||||
@@ -83,4 +77,24 @@ abstract class AppAspect {
|
|||||||
return HttpMessage(Status.Error, message)
|
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
|
@Autowired
|
||||||
lateinit var logger: ILogger
|
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 attributes = RequestContextHolder.getRequestAttributes() as ServletRequestAttributes
|
||||||
val request = attributes.request
|
val request = attributes.request
|
||||||
logger.info(
|
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(
|
mapper.writeValueAsString(
|
||||||
args
|
args
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,6 +2,17 @@ package com.synebula.gaea.spring.aop.handler
|
|||||||
|
|
||||||
import java.lang.reflect.Method
|
import java.lang.reflect.Method
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注解对应的方法处理对象接口
|
||||||
|
*/
|
||||||
interface AnnotationHandler {
|
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