Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

kotlin - 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()}"

question from:https://stackoverflow.com/questions/65842885/using-spel-as-annotation-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...