Search code examples
kotlinspring-elaspect

Using SpEL as annotation value


It's my annotation class:

@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class ExternalCallMonitor(val system: String, val function: String)

Using SpEL to set annotation value as follows;

    @ExternalCallMonitor(system = "#{@url.getHost()}", function = "#{@url.getPath()}")
    fun methodWithUrlParameter(url: URL) {
        // Do nothing
    }

Trying to get value in the Aspect as follows

@Around("@annotation(aspect.external.ExternalCallMonitor)")
@Throws(Throwable::class)
fun externalCallMonitor(joinPoint: ProceedingJoinPoint): Any? {
    val methodName = joinPoint.signature.name
    val signature = joinPoint.signature as MethodSignature
    val method = joinPoint.target.javaClass.getMethod(signature.method.name, URL::class.java)
    val annotation = method.getAnnotation(ExternalCallMonitor::class.java)
    val host = annotation.system

host should be localhost here but I am getting "#{@url.getHost()}"


Solution

  • You need to use a BeanExpressionResolver (e.g. StandardBeanExpressionResolver) to process the SpEL.