Select most specific advice method in case of override

Closes gh-32865
This commit is contained in:
Juergen Hoeller 2024-05-22 10:00:31 +02:00
parent 58da30cd30
commit ea596aa211
2 changed files with 25 additions and 17 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -50,6 +50,7 @@ import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConvertingComparator; import org.springframework.core.convert.converter.ConvertingComparator;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.MethodFilter; import org.springframework.util.ReflectionUtils.MethodFilter;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -133,17 +134,19 @@ public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFacto
List<Advisor> advisors = new ArrayList<>(); List<Advisor> advisors = new ArrayList<>();
for (Method method : getAdvisorMethods(aspectClass)) { for (Method method : getAdvisorMethods(aspectClass)) {
// Prior to Spring Framework 5.2.7, advisors.size() was supplied as the declarationOrderInAspect if (method.equals(ClassUtils.getMostSpecificMethod(method, aspectClass))) {
// to getAdvisor(...) to represent the "current position" in the declared methods list. // Prior to Spring Framework 5.2.7, advisors.size() was supplied as the declarationOrderInAspect
// However, since Java 7 the "current position" is not valid since the JDK no longer // to getAdvisor(...) to represent the "current position" in the declared methods list.
// returns declared methods in the order in which they are declared in the source code. // However, since Java 7 the "current position" is not valid since the JDK no longer
// Thus, we now hard code the declarationOrderInAspect to 0 for all advice methods // returns declared methods in the order in which they are declared in the source code.
// discovered via reflection in order to support reliable advice ordering across JVM launches. // Thus, we now hard code the declarationOrderInAspect to 0 for all advice methods
// Specifically, a value of 0 aligns with the default value used in // discovered via reflection in order to support reliable advice ordering across JVM launches.
// AspectJPrecedenceComparator.getAspectDeclarationOrder(Advisor). // Specifically, a value of 0 aligns with the default value used in
Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, 0, aspectName); // AspectJPrecedenceComparator.getAspectDeclarationOrder(Advisor).
if (advisor != null) { Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, 0, aspectName);
advisors.add(advisor); if (advisor != null) {
advisors.add(advisor);
}
} }
} }

View File

@ -83,15 +83,15 @@ abstract class AbstractAspectJAdvisorFactoryTests {
@Test @Test
void rejectsPerCflowAspect() { void rejectsPerCflowAspect() {
assertThatExceptionOfType(AopConfigException.class) assertThatExceptionOfType(AopConfigException.class)
.isThrownBy(() -> getAdvisorFactory().getAdvisors(aspectInstanceFactory(new PerCflowAspect(), "someBean"))) .isThrownBy(() -> getAdvisorFactory().getAdvisors(aspectInstanceFactory(new PerCflowAspect(), "someBean")))
.withMessageContaining("PERCFLOW"); .withMessageContaining("PERCFLOW");
} }
@Test @Test
void rejectsPerCflowBelowAspect() { void rejectsPerCflowBelowAspect() {
assertThatExceptionOfType(AopConfigException.class) assertThatExceptionOfType(AopConfigException.class)
.isThrownBy(() -> getAdvisorFactory().getAdvisors(aspectInstanceFactory(new PerCflowBelowAspect(), "someBean"))) .isThrownBy(() -> getAdvisorFactory().getAdvisors(aspectInstanceFactory(new PerCflowBelowAspect(), "someBean")))
.withMessageContaining("PERCFLOWBELOW"); .withMessageContaining("PERCFLOWBELOW");
} }
@Test @Test
@ -770,9 +770,15 @@ abstract class AbstractAspectJAdvisorFactoryTests {
} }
} }
@Aspect @Aspect
static class IncrementingAspect extends DoublingAspect { static class IncrementingAspect extends DoublingAspect {
@Override
public Object doubleAge(ProceedingJoinPoint pjp) throws Throwable {
return ((int) pjp.proceed()) * 2;
}
@Around("execution(* getAge())") @Around("execution(* getAge())")
public int incrementAge(ProceedingJoinPoint pjp) throws Throwable { public int incrementAge(ProceedingJoinPoint pjp) throws Throwable {
return ((int) pjp.proceed()) + 1; return ((int) pjp.proceed()) + 1;
@ -780,7 +786,6 @@ abstract class AbstractAspectJAdvisorFactoryTests {
} }
@Aspect @Aspect
private static class InvocationTrackingAspect { private static class InvocationTrackingAspect {