Created
December 15, 2020 09:28
-
-
Save trevorkloz/b824494620a547ae27dd9c97a3c6996e to your computer and use it in GitHub Desktop.
Kotlin Spring Audit Annotation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.backend.audit | |
@Target(AnnotationTarget.FUNCTION) | |
@Retention(AnnotationRetention.RUNTIME) | |
annotation class Audit(val purpose: String) { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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