Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overridden aspect method runs twice #32865

Closed
lexakimov opened this issue May 22, 2024 · 0 comments
Closed

Overridden aspect method runs twice #32865

lexakimov opened this issue May 22, 2024 · 0 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@lexakimov
Copy link

lexakimov commented May 22, 2024

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 22, 2024
@jhoeller jhoeller self-assigned this May 22, 2024
@jhoeller jhoeller added type: bug A general bug in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 22, 2024
@jhoeller jhoeller added this to the 6.1.8 milestone May 22, 2024
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-6.0.x labels May 22, 2024
jhoeller added a commit that referenced this issue May 22, 2024
jhoeller added a commit that referenced this issue May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants