diff --git a/src/gaea.app/build.gradle b/src/gaea.app/build.gradle index fce0f14..5ec0269 100644 --- a/src/gaea.app/build.gradle +++ b/src/gaea.app/build.gradle @@ -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' diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/AnnotationHandler.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/AnnotationHandler.kt new file mode 100644 index 0000000..ca75515 --- /dev/null +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/AnnotationHandler.kt @@ -0,0 +1,7 @@ +package com.synebula.gaea.app.component.aop + +import java.lang.Exception + +interface AnnotationHandler { + fun handle(clazz: Class, func: String, args: Array, exception: Exception?) +} \ No newline at end of file diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/AppAspect.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/AppAspect.kt new file mode 100644 index 0000000..84e9901 --- /dev/null +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/AppAspect.kt @@ -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" + } + } +} \ No newline at end of file diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/annotation/Handler.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/annotation/Handler.kt new file mode 100644 index 0000000..1a1d7ca --- /dev/null +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/annotation/Handler.kt @@ -0,0 +1,5 @@ +package com.synebula.gaea.app.component.aop.annotation + +import kotlin.reflect.KClass + +annotation class Handler(val value: KClass) \ No newline at end of file diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/annotation/SafeExec.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/annotation/SafeExec.kt new file mode 100644 index 0000000..b5b8434 --- /dev/null +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/aop/annotation/SafeExec.kt @@ -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)