From 110b032ad9dddcb643a36b28dba94e4ed548b556 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Fri, 29 Jan 2010 20:55:03 +0000 Subject: [PATCH] IN PROGRESS - issue SPR-6779: imported @Configuration classes do not get enhanced and fail to satisfy scoping requirements All tests in ImportedConfigurationClassEnhancementTests now pass. The fix was simple - imported @Configuration class bean definitions were not getting marked with the attribute that indicates that they are indeed @Configuration class bean definitions. To make this happen, ConfigurationClassPostProcessor's protected checkConfigurationClassCandidate(beanDef) method is being called from within ConfigurationClassBeanDefinitionReader when imported @Configuration classes are being processed. This is quick and dirty, and the subsequent check-in will refactor the solution appropriately. --- ...ConfigurationClassBeanDefinitionReader.java | 1 + .../annotation/ConfigurationClassEnhancer.java | 4 ++-- ...rtedConfigurationClassEnhancementTests.java | 18 +++++++++--------- .../src/test/resources/log4j.xml | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java index 7bf7687753..70669481ad 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java @@ -106,6 +106,7 @@ class ConfigurationClassBeanDefinitionReader { if (configClass.getBeanName() == null) { GenericBeanDefinition configBeanDef = new GenericBeanDefinition(); configBeanDef.setBeanClassName(configClass.getMetadata().getClassName()); + new ConfigurationClassPostProcessor().checkConfigurationClassCandidate(configBeanDef); String configBeanName = BeanDefinitionReaderUtils.registerWithGeneratedName(configBeanDef, this.registry); configClass.setBeanName(configBeanName); if (logger.isDebugEnabled()) { diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java index 93e4efdd75..db62168951 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java @@ -90,8 +90,8 @@ class ConfigurationClassEnhancer { logger.debug("Enhancing " + configClass.getName()); } Class enhancedClass = createClass(newEnhancer(configClass)); - if (logger.isInfoEnabled()) { - logger.info(String.format("Successfully enhanced %s; enhanced class name is: %s", + if (logger.isDebugEnabled()) { + logger.debug(String.format("Successfully enhanced %s; enhanced class name is: %s", configClass.getName(), enhancedClass.getName())); } return enhancedClass; diff --git a/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/ImportedConfigurationClassEnhancementTests.java b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/ImportedConfigurationClassEnhancementTests.java index b23c2d36ea..8ed1c7ee15 100644 --- a/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/ImportedConfigurationClassEnhancementTests.java +++ b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/ImportedConfigurationClassEnhancementTests.java @@ -4,7 +4,6 @@ import static org.hamcrest.CoreMatchers.sameInstance; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -import org.junit.Ignore; import org.junit.Test; import org.springframework.aop.support.AopUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -24,9 +23,9 @@ import test.beans.TestBean; public class ImportedConfigurationClassEnhancementTests { - @Ignore @Test + @Test public void autowiredConfigClassIsEnhancedWhenImported() { - autowiredConfigClassIsEnhanced(ConfigThatImports.class); + autowiredConfigClassIsEnhanced(ConfigThatDoesImport.class); } @Test @@ -37,13 +36,14 @@ public class ImportedConfigurationClassEnhancementTests { private void autowiredConfigClassIsEnhanced(Class... configClasses) { ApplicationContext ctx = new AnnotationConfigApplicationContext(configClasses); Config config = ctx.getBean(Config.class); - assertTrue(AopUtils.isCglibProxyClass(config.autowiredConfig.getClass())); + assertTrue("autowired config class has not been enhanced", + AopUtils.isCglibProxyClass(config.autowiredConfig.getClass())); } - @Ignore @Test + @Test public void autowiredConfigClassBeanMethodsRespectScopingWhenImported() { - autowiredConfigClassBeanMethodsRespectScoping(ConfigThatImports.class); + autowiredConfigClassBeanMethodsRespectScoping(ConfigThatDoesImport.class); } @Test @@ -56,7 +56,8 @@ public class ImportedConfigurationClassEnhancementTests { Config config = ctx.getBean(Config.class); TestBean testBean1 = config.autowiredConfig.testBean(); TestBean testBean2 = config.autowiredConfig.testBean(); - assertThat(testBean1, sameInstance(testBean2)); + assertThat("got two distinct instances of testBean when singleton scoping was expected", + testBean1, sameInstance(testBean2)); } } @@ -68,14 +69,13 @@ class ConfigToBeAutowired { } } - class Config { @Autowired ConfigToBeAutowired autowiredConfig; } @Import(ConfigToBeAutowired.class) @Configuration -class ConfigThatImports extends Config { } +class ConfigThatDoesImport extends Config { } @Configuration class ConfigThatDoesNotImport extends Config { } diff --git a/org.springframework.context/src/test/resources/log4j.xml b/org.springframework.context/src/test/resources/log4j.xml index b09461e5a4..65718d1ae1 100644 --- a/org.springframework.context/src/test/resources/log4j.xml +++ b/org.springframework.context/src/test/resources/log4j.xml @@ -11,7 +11,7 @@ - +