Browse Source

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.
pull/23217/head
Chris Beams 15 years ago
parent
commit
110b032ad9
  1. 1
      org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java
  2. 4
      org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java
  3. 18
      org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/ImportedConfigurationClassEnhancementTests.java
  4. 2
      org.springframework.context/src/test/resources/log4j.xml

1
org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java

@ -106,6 +106,7 @@ class ConfigurationClassBeanDefinitionReader { @@ -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()) {

4
org.springframework.context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java

@ -90,8 +90,8 @@ class ConfigurationClassEnhancer { @@ -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;

18
org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/ImportedConfigurationClassEnhancementTests.java

@ -4,7 +4,6 @@ import static org.hamcrest.CoreMatchers.sameInstance; @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -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 { }

2
org.springframework.context/src/test/resources/log4j.xml

@ -11,7 +11,7 @@ @@ -11,7 +11,7 @@
</layout>
</appender>
<logger name="org.springframework.mapping">
<logger name="org.springframework.context">
<level value="debug" />
</logger>

Loading…
Cancel
Save