Browse Source

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
pull/28631/head
Juergen Hoeller 4 years ago
parent
commit
a14650e0dc
  1. 2
      spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java
  2. 11
      spring-context/src/test/java/org/springframework/context/annotation/configuration/BeanMethodQualificationTests.java

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

@ -745,7 +745,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
if (containsBeanDefinition(beanName)) { if (containsBeanDefinition(beanName)) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName); RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
// Check raw bean class, e.g. in case of a proxy. // Check raw bean class, e.g. in case of a proxy.
if (bd.hasBeanClass()) { if (bd.hasBeanClass() && bd.getFactoryMethodName() == null) {
Class<?> beanClass = bd.getBeanClass(); Class<?> beanClass = bd.getBeanClass();
if (beanClass != beanType) { if (beanClass != beanType) {
MergedAnnotation<A> annotation = MergedAnnotation<A> annotation =

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

@ -141,16 +141,17 @@ class BeanMethodQualificationTests {
assertThat(ctx.getBeanNamesForAnnotation(Configuration.class)).isEqualTo(new String[] {"beanMethodQualificationTests.StandardConfig"}); assertThat(ctx.getBeanNamesForAnnotation(Configuration.class)).isEqualTo(new String[] {"beanMethodQualificationTests.StandardConfig"});
assertThat(ctx.getBeanNamesForAnnotation(Scope.class)).isEqualTo(new String[] {}); assertThat(ctx.getBeanNamesForAnnotation(Scope.class)).isEqualTo(new String[] {});
assertThat(ctx.getBeanNamesForAnnotation(Lazy.class)).isEqualTo(new String[] {"testBean1"}); assertThat(ctx.getBeanNamesForAnnotation(Lazy.class)).isEqualTo(new String[] {"testBean1"});
assertThat(ctx.getBeanNamesForAnnotation(Boring.class)).isEqualTo(new String[] {"testBean2"}); assertThat(ctx.getBeanNamesForAnnotation(Boring.class)).isEqualTo(new String[] {"beanMethodQualificationTests.StandardConfig", "testBean2"});
ctx.close(); ctx.close();
} }
@Configuration @Configuration
@Boring
static class StandardConfig { static class StandardConfig {
@Bean @Qualifier("interesting") @Lazy @Bean @Qualifier("interesting") @Lazy
public TestBean testBean1() { public static TestBean testBean1() {
return new TestBean("interesting"); return new TestBean("interesting");
} }
@ -163,10 +164,11 @@ class BeanMethodQualificationTests {
} }
@Configuration @Configuration
@Boring
static class ScopedConfig { static class ScopedConfig {
@Bean @Qualifier("interesting") @Scope("prototype") @Bean @Qualifier("interesting") @Scope("prototype")
public TestBean testBean1() { public static TestBean testBean1() {
return new TestBean("interesting"); return new TestBean("interesting");
} }
@ -179,10 +181,11 @@ class BeanMethodQualificationTests {
} }
@Configuration @Configuration
@Boring
static class ScopedProxyConfig { static class ScopedProxyConfig {
@Bean @Qualifier("interesting") @Scope(value="prototype", proxyMode=ScopedProxyMode.TARGET_CLASS) @Bean @Qualifier("interesting") @Scope(value="prototype", proxyMode=ScopedProxyMode.TARGET_CLASS)
public TestBean testBean1() { public static TestBean testBean1() {
return new TestBean("interesting"); return new TestBean("interesting");
} }

Loading…
Cancel
Save