Skip to content

Commit de5b9aa

Browse files
committed
Narrow Aware interface exclusion check to BeanFactoryAware only
Closes gh-35835
1 parent f72891c commit de5b9aa

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractFallbackJCacheOperationSource.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.jspecify.annotations.Nullable;
2727

2828
import org.springframework.aop.support.AopUtils;
29-
import org.springframework.beans.factory.Aware;
29+
import org.springframework.beans.factory.BeanFactoryAware;
3030
import org.springframework.core.MethodClassKey;
3131
import org.springframework.util.ReflectionUtils;
3232

@@ -95,8 +95,8 @@ else if (cacheNull) {
9595
if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
9696
return null;
9797
}
98-
// Skip methods declared on BeanFactoryAware and co.
99-
if (method.getDeclaringClass().isInterface() && Aware.class.isAssignableFrom(method.getDeclaringClass())) {
98+
// Skip setBeanFactory method on BeanFactoryAware.
99+
if (method.getDeclaringClass() == BeanFactoryAware.class) {
100100
return null;
101101
}
102102

spring-context/src/main/java/org/springframework/cache/interceptor/AbstractFallbackCacheOperationSource.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.jspecify.annotations.Nullable;
2929

3030
import org.springframework.aop.support.AopUtils;
31-
import org.springframework.beans.factory.Aware;
31+
import org.springframework.beans.factory.BeanFactoryAware;
3232
import org.springframework.core.MethodClassKey;
3333
import org.springframework.util.ClassUtils;
3434
import org.springframework.util.CollectionUtils;
@@ -137,8 +137,8 @@ protected Object getCacheKey(Method method, @Nullable Class<?> targetClass) {
137137
if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
138138
return null;
139139
}
140-
// Skip methods declared on BeanFactoryAware and co.
141-
if (method.getDeclaringClass().isInterface() && Aware.class.isAssignableFrom(method.getDeclaringClass())) {
140+
// Skip setBeanFactory method on BeanFactoryAware.
141+
if (method.getDeclaringClass() == BeanFactoryAware.class) {
142142
return null;
143143
}
144144

spring-tx/src/main/java/org/springframework/transaction/interceptor/AbstractFallbackTransactionAttributeSource.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.jspecify.annotations.Nullable;
2727

2828
import org.springframework.aop.support.AopUtils;
29-
import org.springframework.beans.factory.Aware;
29+
import org.springframework.beans.factory.BeanFactoryAware;
3030
import org.springframework.context.EmbeddedValueResolverAware;
3131
import org.springframework.core.MethodClassKey;
3232
import org.springframework.util.ClassUtils;
@@ -163,8 +163,8 @@ protected Object getCacheKey(Method method, @Nullable Class<?> targetClass) {
163163
if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
164164
return null;
165165
}
166-
// Skip methods declared on BeanFactoryAware and co.
167-
if (method.getDeclaringClass().isInterface() && Aware.class.isAssignableFrom(method.getDeclaringClass())) {
166+
// Skip setBeanFactory method on BeanFactoryAware.
167+
if (method.getDeclaringClass() == BeanFactoryAware.class) {
168168
return null;
169169
}
170170

spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSourceTests.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import org.springframework.aop.framework.Advised;
3434
import org.springframework.aop.framework.ProxyFactory;
35+
import org.springframework.beans.factory.BeanNameAware;
3536
import org.springframework.core.annotation.AliasFor;
3637
import org.springframework.core.annotation.AnnotationUtils;
3738
import org.springframework.core.testfixture.io.SerializationTestUtils;
@@ -59,6 +60,7 @@ class AnnotationTransactionAttributeSourceTests {
5960

6061
private final AnnotationTransactionAttributeSource attributeSource = new AnnotationTransactionAttributeSource();
6162

63+
6264
@Test
6365
void serializable() throws Exception {
6466
TestBean1 tb = new TestBean1();
@@ -123,6 +125,10 @@ void transactionAttributeDeclaredOnCglibClassMethod() {
123125
void transactionAttributeDeclaredOnInterfaceMethodOnly() {
124126
TransactionAttribute actual = getTransactionAttribute(TestBean2.class, ITestBean2.class, "getAge");
125127
assertThat(actual).satisfies(hasNoRollbackRule());
128+
actual = getTransactionAttribute(TestBean2.class, ITestBean2X.class, "getAge");
129+
assertThat(actual).satisfies(hasNoRollbackRule());
130+
actual = getTransactionAttribute(ITestBean2X.class, ITestBean2X.class, "getAge");
131+
assertThat(actual).satisfies(hasNoRollbackRule());
126132
}
127133

128134
/**
@@ -249,6 +255,7 @@ void customMethodAttributeWithReadOnlyOverrideOnInterface() {
249255
assertThat(actual.isReadOnly()).isTrue();
250256
}
251257

258+
252259
@Nested
253260
class JtaAttributeTests {
254261

@@ -276,6 +283,7 @@ void transactionAttributeDeclaredOnInterface() {
276283
assertThat(getNameAttr.getPropagationBehavior()).isEqualTo(TransactionAttribute.PROPAGATION_SUPPORTS);
277284
}
278285

286+
279287
static class JtaAnnotatedBean1 implements ITestBean1 {
280288

281289
private String name;
@@ -305,7 +313,6 @@ public void setAge(int age) {
305313
}
306314
}
307315

308-
309316
@jakarta.transaction.Transactional(jakarta.transaction.Transactional.TxType.SUPPORTS)
310317
static class JtaAnnotatedBean2 implements ITestBean1 {
311318

@@ -362,7 +369,6 @@ public void setAge(int age) {
362369
}
363370
}
364371

365-
366372
@jakarta.transaction.Transactional(jakarta.transaction.Transactional.TxType.SUPPORTS)
367373
interface ITestJta {
368374

@@ -375,9 +381,9 @@ interface ITestJta {
375381

376382
void setName(String name);
377383
}
378-
379384
}
380385

386+
381387
@Nested
382388
class Ejb3AttributeTests {
383389

@@ -448,7 +454,6 @@ public void setAge(int age) {
448454
}
449455
}
450456

451-
452457
@jakarta.ejb.TransactionAttribute(TransactionAttributeType.SUPPORTS)
453458
static class Ejb3AnnotatedBean2 implements ITestBean1 {
454459

@@ -506,6 +511,7 @@ public void setAge(int age) {
506511
}
507512
}
508513

514+
509515
@Nested
510516
class GroovyTests {
511517

@@ -519,6 +525,7 @@ void transactionAttributeDeclaredOnGroovyClass() {
519525
assertThat(attributeSource.getTransactionAttribute(getMetaClassMethod, GroovyTestBean.class)).isNull();
520526
}
521527

528+
522529
@Transactional
523530
static class GroovyTestBean implements ITestBean1, GroovyObject {
524531

@@ -571,6 +578,7 @@ public void setMetaClass(MetaClass metaClass) {
571578
}
572579
}
573580

581+
574582
private Consumer<TransactionAttribute> hasRollbackRules(RollbackRuleAttribute... rollbackRuleAttributes) {
575583
return transactionAttribute -> {
576584
RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
@@ -626,7 +634,12 @@ interface ITestBean2 {
626634
}
627635

628636

629-
interface ITestBean2X extends ITestBean2 {
637+
interface ITestBean2X extends ITestBean2, BeanNameAware {
638+
639+
@Transactional
640+
int getAge();
641+
642+
void setAge(int age);
630643

631644
String getName();
632645

@@ -735,6 +748,10 @@ public TestBean2(String name, int age) {
735748
this.age = age;
736749
}
737750

751+
@Override
752+
public void setBeanName(String name) {
753+
}
754+
738755
@Override
739756
public String getName() {
740757
return name;
@@ -917,6 +934,7 @@ public int getAge() {
917934
}
918935
}
919936

937+
920938
@Transactional(label = {"retryable", "long-running"})
921939
static class TestBean11 {
922940

0 commit comments

Comments
 (0)