增加注解处理

This commit is contained in:
2021-03-19 16:52:03 +08:00
parent c209cede2b
commit 0b5d9243e8
5 changed files with 91 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
dependencies {
compile project(":src:gaea")
compile("org.springframework.boot:spring-boot-starter-web:$spring_version")
compile("org.springframework.boot:spring-boot-starter-aop:$spring_version")
compile("org.springframework.boot:spring-boot-starter-mail:$spring_version")
compile group: 'net.sf.dozer', name: 'dozer', version: '5.5.1'
compile group: 'org.apache.poi', name: 'poi', version: '4.1.2'

View File

@@ -0,0 +1,7 @@
package com.synebula.gaea.app.component.aop
import java.lang.Exception
interface AnnotationHandler {
fun handle(clazz: Class<Any>, func: String, args: Array<Any>, exception: Exception?)
}

View File

@@ -0,0 +1,68 @@
package com.synebula.gaea.app.component.aop
import com.synebula.gaea.app.component.HttpMessage
import com.synebula.gaea.app.component.aop.annotation.SafeExec
import com.synebula.gaea.data.message.Status
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.*
import org.springframework.stereotype.Component
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.ServletRequestAttributes
import java.util.*
@Aspect
@Component
class AppAspect {
@Pointcut("within(com.synebula..app.*)")
fun log() {
}
//后置异常通知
@AfterThrowing("log()")
fun throws(point: JoinPoint): Any {
val clazz = point.signature.declaringType
println("${clazz.name} - ${point.signature.name}异常:${point.signature.name}")
return "error"
}
//后置最终通知,final增强不管是抛出异常或者正常退出都会执行
@After("log()")
fun after(point: JoinPoint) {
println("方法最后执行.....")
}
//环绕通知,环绕增强相当于MethodInterceptor
@Around("log()")
fun around(point: ProceedingJoinPoint): Any? {
val clazz = point.signature.declaringType
val func = clazz.methods.find {
it.name == point.signature.name
}!!
val clazzAnnotations = clazz.annotations
val funcAnnotations = func.annotations ?: arrayOf()
val attributes = RequestContextHolder.getRequestAttributes() as ServletRequestAttributes
val request = attributes.request
// 记录下请求内容
println("URL : " + request.requestURL.toString())
println("HTTP_METHOD : " + request.method)
println("IP : " + request.remoteAddr)
println("CLASS_METHOD : " + clazz.name + "." + point.signature.name)
println("ARGS : " + Arrays.toString(point.args))
return try {
val res = point.proceed()
res
} catch (ex: Throwable) {
val msg = HttpMessage(Status.Success)
for (item in funcAnnotations) {
if (item.annotationClass == SafeExec::javaClass) {
}
}
println("方法${point.signature}异常: $msg - ${ex.message}")
return "error"
}
}
}

View File

@@ -0,0 +1,5 @@
package com.synebula.gaea.app.component.aop.annotation
import kotlin.reflect.KClass
annotation class Handler(val value: KClass<Any>)

View File

@@ -0,0 +1,10 @@
package com.synebula.gaea.app.component.aop.annotation
/**
* 标记方法安全执行由AOP负责try catch异常
*
* @param errorMessage 异常消息
*/
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class SafeExec(val errorMessage: String)