Skip to content

Instantly share code, notes, and snippets.

@trevorkloz
Created December 15, 2020 09:28
Show Gist options
  • Save trevorkloz/b824494620a547ae27dd9c97a3c6996e to your computer and use it in GitHub Desktop.
Save trevorkloz/b824494620a547ae27dd9c97a3c6996e to your computer and use it in GitHub Desktop.
Kotlin Spring Audit Annotation
package com.example.backend.audit
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class Audit(val purpose: String) {
}
package com.example.backend.audit
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.reflect.MethodSignature
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import java.util.*
@Aspect
@Component
class AuditAspect {
private val logger = LoggerFactory.getLogger(AuditAspect::class.java)
@Around("@annotation(com.example.backend.audit.Audit)")
fun auditExecution(joinPoint: ProceedingJoinPoint): Any? {
val data = joinPoint.proceed()
val input = joinPoint.args[0] as Any
val annotation = (joinPoint.signature as MethodSignature).method.getAnnotation(Audit::class.java)
// do some stuff...
return data
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment