Skip to content

Provide bean conditional registration capabilities in BeanRegistrar #21497

Closed

Description

@spring-projects-issues

Dave Syer opened SPR-16959 and commented

There is a gap in the the functional bean registration features in BeanDefinitionBuilder and GenericApplicationContext. Once you register a Supplier you have committed to provide an instance of the class you register, whereas in a lot of use cases you don't know whether or not you want to provide it until the bean factory is available (e.g. conditional on another instance of the same class being available). I guess the change needs to be in the BeanDefinitionRegistry interface, for example to support a Predicate<ConditionContext> as well as the Supplier. If the interface doesn't change, I suppose returning null from the Supplier might be an option, but that seems a bit ugly, and might be too late, since the BeanDefinition has already been registered at that point.


Issue Links:

Activity

  1. spring-projects-issues commented on Jun 20, 2018

    @spring-projects-issues
    CollaboratorAuthor

    Andy Wilkinson commented

    I've been experimenting a bit with conditions in functional bean registration building on top of Framework. It has become apparent that we really need support at the Framework level so that condition evaluation and bean registration can occur at the right time. As Dave alludes to above, the biggest stumbling block at the moment is with bean-based conditions where we'd need a callback at the point where @Configuration class parsing is performing bean registration. This would, I think, allow a mixture of functional-based and annotation-based conditional registration in the same application context.

    As things stand, condition evaluation is tied to the @Configuration programming model. It feels to me like this could be generalised so that @Configuration classes are just one source of conditional beans. Rather than driving the registration from ConfigurationClassPostProcessor it could be driven from something general that calls multiple different types of sources of conditional beans. That might result in a new callback that's specifically intended for functional bean registration, rather than the current approach that typically seems to involve the use of the more general ApplicationContextInitializer which doesn't have a well-defined lifecycle for when it's called.

    The thoughts above have come from some experimentation and prototyping for reducing memory usage and startup time of Boot apps. Functional registration appears to be one way of doing that but we're quite a way short of declaring that it's the way to do it. As such, we (the Boot team) don't have a concrete need for this functionality just yet, although I do think that conditional beans becoming a broader concept, rather than being confined to the annotation-based programming model as they are today, would be generally useful beyond what Boot may or may not need.

  2. added
    type: enhancement
    in: core
    on Jan 11, 2019
  3. added
    status: pending-design-work
    status: on-hold
    on Oct 25, 2023
  4. removed their assignment
    on Dec 11, 2023
  5. removed their assignment
    on Mar 25, 2024
  6. changed the title [-]Support for conditional registration of functional bean definitions [SPR-16959][/-] [+]Support for conditional registration of functional bean definitions[/+] on Mar 6, 2025
  7. sdeleuze commented on Mar 6, 2025

    @sdeleuze
    Contributor

    Spring Framework 7 introduces a new BeanRegistrar contract that allows to combine @Conditional (and variants, likely including Spring Boot ones even if I have not yet tested) and programmatic conditions, see #18353 (comment).

    We should explore if more is needed or if that's flexible enough.

  8. removed
    status: pending-design-work
    on Mar 10, 2025
  9. 9 remaining items

  10. sdeleuze commented on Mar 21, 2025

    @sdeleuze
    Contributor

    So we discussed the Spring Security DSL use case with @rwinch who proposed a really interesting idea where Spring Security DSL could be used inside a BeanRegistrar implementation written by the user. I refined it a bit with an abstract class that Spring Security could provide, and that gives something like below which is already supported in what has been shipped in Spring Framework 7.0.0-M3 and does not require additional conditions. cc @wakingrufus @OlgaMaciaszek @rstoyanchev @dsyer

    @Configuration
    @Import(MySecurity.class)
    class MyConfiguration {
    }
    
    class MySecurity extends SecurityDsl {
    
        @Override
        public void register(HttpSecurity registry, Environment env) {
            http.formLogin();
        }
    }
    
    // Could be provided by Spring Security
    abstract class SecurityDsl implements BeanRegistrar {
    
    
        public abstract void register(HttpSecurity http, Environment env);
    
        @Override
        public void register(BeanRegistry registry, Environment env) {
            HttpSecurity http = new HttpSecurity();
            register(http, env);
    
            if (http.hasFormLogin()) {
                registry.registerBean(...);
            }
            // ...
        }
    }

    For other conditions, as discussed recently we would prefer to give more time post Framework 7.0 / Boot 4.0 GA to the Spring Boot team to provide feedback on potential programmatic equivalent of @ConditionalOnBean, @ConditionalOnMissingBean and similar advanced capabilities.

    For classpath checks, it is unclear if we should let users just use if statements with ClassUtils#isPresent, if we should expose a ClassLoader or if we should provide something like BeanRegistry#isClassPresent(String). As we don't have a clear use case for now, I am leaning to wait for Spring Boot team feedback post GA as well.

    As a consequence, I move back this issue to the general backlog for potential exploration in Spring Framework 7.x after the 7.0 GA in collaboration with the Boot team. Does not prevent to add specific capabilities before 7.0.0 if some very well defined needs arise via more specific issues.

  11. changed the title [-]Support for conditional registration of functional bean definitions[/-] [+]Provide additional conditional registration capabilities in `BeanRegistrar`[/+] on Mar 21, 2025
  12. OlgaMaciaszek commented on Mar 21, 2025

    @OlgaMaciaszek
    Contributor

    The ability to work with DSL looks very interesting and might help us to work around the lifecycle issues for users interacting directly with Interface Clients Registry (CC @rstoyanchev ). Will try it out and provide any feedback that we have.

  13. DanielLiu1123 commented on Sep 17, 2025

    @DanielLiu1123

    +1

  14. self-assigned this
    on Mar 26, 2026
  15. jhoeller commented on Mar 28, 2026

    @jhoeller
    Contributor

    I've introduced a DeferredBeanRegistrar variant for 7.1, along with various containsBean methods on BeanRegistry.

    The containsBean methods are just able to expose an intermediate registration state with a regular BeanRegistrar which is to be invoked immediately at the time of its registration. However, they will provide reliable answers with a DeferredBeanRegistrar which is to be invoked at the end of the registration phase, independent of the order of user bean registration versus BeanRegistrar import/registration. This allows for DeferredBeanRegistrar implementations with custom containsBean checks in if statements, seeing all user-registered beans at that point, reacting to the overall state of the user configuration.

  16. added a commit that references this issue on Mar 28, 2026
    7502b92
  17. added a commit that references this issue on Apr 1, 2026
    3bf31e4
  18. jhoeller commented on Apr 1, 2026

    @jhoeller
    Contributor

    Based on feedback from the Boot team, I've replaced DeferredBeanRegistrar with implicit ordering semantics: GenericApplicationContext-registered BeanRegistrars are invoked after other programmatic bean definitions. Configuration-imported BeanRegistrars participate in configuration class order and in particular in Boot's auto-configuration ordering.

  19. changed the title [-]Provide additional conditional registration capabilities in `BeanRegistrar`[/-] [+]Provide bean conditional registration capabilities in `BeanRegistrar`[/+] on Apr 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

in: coretype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions