Skip to content

Adjust registration order of BeanDefinition declared by @Configuration #35008

Closed as not planned

Description

@xinbimingjingtai

While I tried to customize an AutoConfiguration, but @ConditionalOnBean did not working, then I tried to find the problem through examples, source code, and the Internet, and used different configuration methods, and finally found that it was because of the registration order of BeanDefinition.

In my opinion, their registration order should be like this:

  1. Configuration
  2. beans annotated on Configuration (e.g. @Import and @EnableConfigurationProperties)
  3. (static) InnerConfiguration
  4. @bean method

Then about ConfigurationClassParser, the (static) InnerConfiguration should be managed by the fields in ConfigurationClass of Configuration, and if the Conditional of Configuration is not matched, then InnerConfiguration should also be skipped, ConfigurationClassBeanDefinitionReader will be like:

private void loadBeanDefinitionsForConfigurationClass(
			ConfigurationClass configClass, TrackedConditionEvaluator trackedConditionEvaluator) {

		if (trackedConditionEvaluator.shouldSkip(configClass)) {
			String beanName = configClass.getBeanName();
			if (StringUtils.hasLength(beanName) && this.registry.containsBeanDefinition(beanName)) {
				this.registry.removeBeanDefinition(beanName);
			}
			this.importRegistry.removeImportingClass(configClass.getMetadata().getClassName());
			return;
		}

		if (configClass.isImported()) {
			registerBeanDefinitionForImportedConfigurationClass(configClass);
		}
                 
		loadBeanDefinitionsFromImportedResources(configClass.getImportedResources());
		loadBeanDefinitionsFromRegistrars(configClass.getImportBeanDefinitionRegistrars());
		loadBeanDefinitionsFromInnerConfigurationClass(configClass.getInnerConfigurationClass()); // something like this

		for (BeanMethod beanMethod : configClass.getBeanMethods()) {
			loadBeanDefinitionsForBeanMethod(beanMethod);
		}
	}

demo2.zip

Activity

  1. bclozel commented on Jun 7, 2025

    @bclozel
    Member

    Let's take a step back.
    Instead of describing the implementation changes you want, can you explain what are you trying to achieve and why it doesn't work?

  2. added
    in: coreIssues in core modules (aop, beans, core, context, expression)
    on Jun 7, 2025
  3. xinbimingjingtai commented on Jun 8, 2025

    @xinbimingjingtai
    Author

    Let's take a step back. Instead of describing the implementation changes you want, can you explain what are you trying to achieve and why it doesn't work?

    Let me simplify it first, please check the code below:

    @ConditionalOnClass(FooClient.class)
    @ConditionalOnMissingBean(FooService.class)
    @EnableConfigurationProperties(FooProperties.class)
    @Configuration(proxyBeanMethods = false)
    public class FooServiceAutoConfiguration {
    
        public FooServiceAutoConfiguration() {
        }
    
        @ConditionalOnBean(FooProperties.class)
        @ConditionalOnMissingBean(FooClientFactory.class)
        @Bean
        public FooClientFactory fooClientFactory(FooProperties fooProperties) {
            return new FooClientFactory(fooProperties);
        }
    
        @ConditionalOnBean({
                FooProperties.class,
                FooClientFactory.class
        })
        @ConditionalOnMissingBean(FooService.class)
        @Bean
        public FooService fooService(FooProperties fooProperties, FooClientFactory fooClientFactory) {
            return new FooServiceImpl(fooProperties, fooClientFactory);
        }
    
    }

    Its execution order is as follows:

    1. parse the ConfigurationClass instance of FooServiceAutoConfiguration through ConfigurationClassParser, and registered BeanDefinition
    2. load BeanDefinition by ConfigurationClassBeanDefinitionReader
      2.1 check if FooServiceAutoConfiguration shouldSkip
      2.2 registerBeanDefinitionForImportedConfigurationClass: annotation @Import
      2.3 loadBeanDefinitionsForBeanMethod: annotation @Bean
      2.4 loadBeanDefinitionsFromImportedResources
      2.5 loadBeanDefinitionsFromRegistrars(renamed to loadBeanDefinitionsFromImportBeanDefinitionRegistrars in 7.x): annotation @EnableConfigurationProperties will be handled here

    But, in step 2.3 loadBeanDefinitionsForBeanMethod, while check the conditional of @ConditionalOnBean(FooProperties.class), it found that there is no bean(or BeanDefinitation) for type FooProperties, because the BeanDefinition of type FooProperties load in step 2.5 loadBeanDefinitionsFromRegistrars.

    I knew that if I remove @ConditionalOnBean(...), then it's will work well, but I still think it is a little strange that it cannot create beans when the condition exists. Would you or someone in spring team be willing to tell me the reason for this design?

  4. changed the title [-]Adjus registration order of BeanDefinition declared by @Configuration[/-] [+]Adjust registration order of BeanDefinition declared by @Configuration[/+] on Jan 9, 2026
  5. bclozel commented on Oct 23, 2026

    @bclozel
    Member

    Thanks for raising this, and sorry it has gone so long without a decision.

    We're reviewing older issues to keep the tracker focused on work we can realistically plan. This request hasn't drawn much interest from the community and doesn't fit the current priorities, so we're declining it.

    If it still matters to you on a current Spring Framework version, please add a comment with your use case and we can reconsider. A reproducer or concrete examples help a lot.

  6. added
    status: declinedA suggestion or change that we don't feel we should currently apply
    and removed on Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions