Skip to content

Commit a14650e

Browse files
committed
findAnnotationOnBean only falls back to bean class in case of no factory method
Most importantly, static @bean methods do not expose their @configuration class-level annotations anymore, aligned with the behavior for non-static @bean methods. Closes gh-28298
1 parent 083113d commit a14650e

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ private <A extends Annotation> MergedAnnotation<A> findMergedAnnotationOnBean(
745745
if (containsBeanDefinition(beanName)) {
746746
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
747747
// Check raw bean class, e.g. in case of a proxy.
748-
if (bd.hasBeanClass()) {
748+
if (bd.hasBeanClass() && bd.getFactoryMethodName() == null) {
749749
Class<?> beanClass = bd.getBeanClass();
750750
if (beanClass != beanType) {
751751
MergedAnnotation<A> annotation =

spring-context/src/test/java/org/springframework/context/annotation/configuration/BeanMethodQualificationTests.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,17 @@ void beanNamesForAnnotation() {
141141
assertThat(ctx.getBeanNamesForAnnotation(Configuration.class)).isEqualTo(new String[] {"beanMethodQualificationTests.StandardConfig"});
142142
assertThat(ctx.getBeanNamesForAnnotation(Scope.class)).isEqualTo(new String[] {});
143143
assertThat(ctx.getBeanNamesForAnnotation(Lazy.class)).isEqualTo(new String[] {"testBean1"});
144-
assertThat(ctx.getBeanNamesForAnnotation(Boring.class)).isEqualTo(new String[] {"testBean2"});
144+
assertThat(ctx.getBeanNamesForAnnotation(Boring.class)).isEqualTo(new String[] {"beanMethodQualificationTests.StandardConfig", "testBean2"});
145145
ctx.close();
146146
}
147147

148148

149149
@Configuration
150+
@Boring
150151
static class StandardConfig {
151152

152153
@Bean @Qualifier("interesting") @Lazy
153-
public TestBean testBean1() {
154+
public static TestBean testBean1() {
154155
return new TestBean("interesting");
155156
}
156157

@@ -163,10 +164,11 @@ public TestBean testBean2(@Lazy TestBean testBean1) {
163164
}
164165

165166
@Configuration
167+
@Boring
166168
static class ScopedConfig {
167169

168170
@Bean @Qualifier("interesting") @Scope("prototype")
169-
public TestBean testBean1() {
171+
public static TestBean testBean1() {
170172
return new TestBean("interesting");
171173
}
172174

@@ -179,10 +181,11 @@ public TestBean testBean2(TestBean testBean1) {
179181
}
180182

181183
@Configuration
184+
@Boring
182185
static class ScopedProxyConfig {
183186

184187
@Bean @Qualifier("interesting") @Scope(value="prototype", proxyMode=ScopedProxyMode.TARGET_CLASS)
185-
public TestBean testBean1() {
188+
public static TestBean testBean1() {
186189
return new TestBean("interesting");
187190
}
188191

0 commit comments

Comments
 (0)