Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading @Import(ChildConfig) or EnclosingConfig.ChildConfig and then ChildConfig does not load beans on ParentConfig [SPR-10546] #15176

Closed
spring-projects-issues opened this issue May 9, 2013 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented May 9, 2013

Rob Winch opened SPR-10546 and commented

Given the following configuration:

@Configuration
static class ParentConfig {
  @Bean
  public String myBean() {
    return "myBean";
  }
}

@Configuration
static class EnclosingConfig {
  @Configuration
  public static class ChildConfig extends ParentConfig {}
}

The following will succeed:

AnnotationConfigApplicationContext context = 
  new AnnotationConfigApplicationContext(EnclosingConfig.ChildConfig.class,EnclosingConfig.class);
context.getBean(String.class)

The following will fail:

AnnotationConfigApplicationContext context = 
  new AnnotationConfigApplicationContext(EnclosingConfig.class, EnclosingConfig.ChildConfig.class);
context.getBean(String.class)

The reason this fails is the following happens:

  • ConfigurationClassParser#doProcessConfigurationClass parses EnclosingConfig
  • It then processes the member classes which includes EnclosingConfig.ChildConfig
  • ConfigurationClassParser#doProcessConfigurationClass parses EnclosingConfig.ChildConfig
  • ConfigurationClassParser#doProcessConfigurationClas parses EnclosingConfig.ChildConfig's parent class ParentConfig
  • myBean is added as a method to EnclosingConfig.ChildConfig's configClass
  • ParentConfig is marked to not be processed again by adding it to knownSuperclasses
  • ConfigurationClassParser#doProcessConfigurationClass parses EnclosingConfig.ChildConfig and does not attempt to process the super class because it is already in knownSuperclasses. Because of this, the configClass with this ConfigurationClass does not have any beanMethods from the super class
  • ConfigurationClassParser.processConfigurationClass removes the existing instance of ConfigurationClass which has the beanMethods form ParentConfig on it
  • The EnclosingConfig.ChildConfig that does not have the beanMethods from the ParentConfig on it is then added to the configurationClasses

It might be relevant to note that the failure and success depends on if the first @Configuration has name or not. If second Configuration does not have a bean name, then it is not overridden.

Another example would be given:

@Configuration
static class ParentConfig {
  @Bean
  public String myBean() {
    return "myBean";
  }
}
@Configuration
static class ChildConfig extends ParentConfig {}

@Configuration
@Import(ChildConfig.class)
static class ImportChildConfig {}

The following will succeed:

AnnotationConfigApplicationContext context = 
  new AnnotationConfigApplicationContext(ChildConfig.class,ImportChildConfig.class);
context.getBean(String.class)

The following will fail:

AnnotationConfigApplicationContext context = 
  new AnnotationConfigApplicationContext(ImportChildConfig.class, ChildConfig.class);
context.getBean(String.class)

Please note that this issue can happen when using classpath scanning, so this can be quite difficult to track down if the classes are discovered in a different order. The classpath scanning (at least for my system) seems to be implemented to order the classes alphabetically. I have not dug into the internals of the classpath scanning implementation to determine if this is environment specific or not.


Affects: 3.2.2

Issue Links:

Referenced from: commits 6e4317e, 3f7007f, d1859c8, 940011e

0 votes, 5 watchers

@spring-projects-issues
Copy link
Collaborator Author

Rob Winch commented

Pull request submitted #282

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants