Skip to content

Overridden aspect method runs twice #32865

Closed

Description

@lexakimov

Affects: 6.1.6

If you extend an aspect class and override its method, then this method will be called twice.

Example in kotlin:

import mu.KLogging
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.stereotype.Component

annotation class CustomAnnotation()

@Aspect
open class CustomAnnotationAspect {
    @Around("execution (@CustomAnnotation * *.*(..))")
    open fun wrapExecution(pjp: ProceedingJoinPoint): Any? {
        logger.info { "start in super class" }
        val result = pjp.proceed()
        logger.info { "start in super class" }
        return result
    }

    companion object : KLogging()
}

@Aspect
class ExtendedCustomAnnotationAspect : CustomAnnotationAspect() {
    // don't matter if same @Around annotation presents or not
    override fun wrapExecution(pjp: ProceedingJoinPoint): Any? {
        logger.info { "start" }
        val result = pjp.proceed()
        logger.info { "finish" }
        return result
    }

    companion object : KLogging()
}


@Configuration
class Config {
    @Bean
    fun customAspect(): ExtendedCustomAnnotationAspect {
        return ExtendedCustomAnnotationAspect()
    }
}

then, when we call method annotated with @CustomAnnotation, we get output in console:

start
start
target method execution
finish
finish

expected output:

start
target method execution
finish

The call of ReflectionUtils.doWithMethods(...) in ReflectiveAspectJAdvisorFactory#getAdvisorMethods collects all user declared methods (methods of superclass and overridden methods), and all of them are used for making an advisors.

Снимок экрана_20240522_132928

Activity

  1. self-assigned this
    on May 22, 2024
  2. added
    in: coreIssues in core modules (aop, beans, core, context, expression)
    and removed on May 22, 2024
  3. added this to the 6.1.8 milestone on May 22, 2024
  4. added a commit that references this issue on May 22, 2024
    ea596aa
  5. added 2 commits that reference this issue on May 22, 2024
    e73d68b
    eff9f5b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions