From 1fbd04700355dfe91a73965d47ee4dadccb8e80b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 1 Aug 2017 12:05:26 +0200 Subject: [PATCH] checkConfigurationClassCandidate explicitly skips factory method definitions Issue: SPR-14603 --- .../annotation/ConfigurationClassUtils.java | 2 +- .../ConfigurationClassPostProcessorTests.java | 36 +++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java index f5117f54e8..97f77b1cc6 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassUtils.java @@ -79,7 +79,7 @@ abstract class ConfigurationClassUtils { */ public static boolean checkConfigurationClassCandidate(BeanDefinition beanDef, MetadataReaderFactory metadataReaderFactory) { String className = beanDef.getBeanClassName(); - if (className == null) { + if (className == null || beanDef.getFactoryMethodName() != null) { return false; } diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java index 533b2fb1ae..498da1c429 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java @@ -43,6 +43,9 @@ import org.springframework.beans.factory.annotation.Lookup; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver; import org.springframework.beans.factory.config.BeanDefinitionHolder; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.beans.factory.support.ChildBeanDefinition; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; @@ -806,6 +809,12 @@ public class ConfigurationClassPostProcessorTests { ctx.getBean("myTestBean", TestBean.class); } + @Test + public void testBeanDefinitionRegistryPostProcessorConfig() { + ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanDefinitionRegistryPostProcessorConfig.class); + assertTrue(ctx.getBean("myTestBean") instanceof TestBean); + } + // ------------------------------------------------------------------------- @@ -902,8 +911,7 @@ public class ConfigurationClassPostProcessorTests { @Configuration static class UnloadedConfig { - public @Bean - Foo foo() { + public @Bean Foo foo() { return new Foo(); } } @@ -911,8 +919,7 @@ public class ConfigurationClassPostProcessorTests { @Configuration static class LoadedConfig { - public @Bean - Bar bar() { + public @Bean Bar bar() { return new Bar(new Foo()); } } @@ -926,9 +933,7 @@ public class ConfigurationClassPostProcessorTests { @Configuration public static class ScopedProxyConfigurationClass { - @Bean - @Lazy - @Scope(proxyMode = ScopedProxyMode.INTERFACES) + @Bean @Lazy @Scope(proxyMode = ScopedProxyMode.INTERFACES) public ITestBean scopedClass() { return new TestBean(); } @@ -1517,4 +1522,21 @@ public class ConfigurationClassPostProcessorTests { public abstract TestBean getTestBean(); } + @Configuration + static class BeanDefinitionRegistryPostProcessorConfig { + + @Bean + public static BeanDefinitionRegistryPostProcessor bdrpp() { + return new BeanDefinitionRegistryPostProcessor() { + @Override + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) { + registry.registerBeanDefinition("myTestBean", new RootBeanDefinition(TestBean.class)); + } + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { + } + }; + } + } + }