You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Provide bean conditional registration capabilities in BeanRegistrar #21497
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.
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.
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
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.
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)
classMyConfiguration {
}
classMySecurityextendsSecurityDsl {
@Overridepublicvoidregister(HttpSecurityregistry, Environmentenv) {
http.formLogin();
}
}
// Could be provided by Spring SecurityabstractclassSecurityDslimplementsBeanRegistrar {
publicabstractvoidregister(HttpSecurityhttp, Environmentenv);
@Overridepublicvoidregister(BeanRegistryregistry, Environmentenv) {
HttpSecurityhttp = newHttpSecurity();
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.
changed the title [-]Support for conditional registration of functional bean definitions[/-][+]Provide additional conditional registration capabilities in `BeanRegistrar`[/+]on Mar 21, 2025
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.
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.
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.
changed the title [-]Provide additional conditional registration capabilities in `BeanRegistrar`[/-][+]Provide bean conditional registration capabilities in `BeanRegistrar`[/+]on Apr 5, 2026
Activity
spring-projects-issues commentedon Jun 20, 2018
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
@Configurationclass 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
@Configurationprogramming model. It feels to me like this could be generalised so that@Configurationclasses are just one source of conditional beans. Rather than driving the registration fromConfigurationClassPostProcessorit 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 generalApplicationContextInitializerwhich 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.
[-]Support for conditional registration of functional bean definitions [SPR-16959][/-][+]Support for conditional registration of functional bean definitions[/+]sdeleuze commentedon Mar 6, 2025
Spring Framework 7 introduces a new
BeanRegistrarcontract 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.
9 remaining items
sdeleuze commentedon Mar 21, 2025
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
BeanRegistrarimplementation 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 Framework7.0.0-M3and does not require additional conditions. cc @wakingrufus @OlgaMaciaszek @rstoyanchev @dsyerFor 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,@ConditionalOnMissingBeanand 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 aClassLoaderor if we should provide something likeBeanRegistry#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.
[-]Support for conditional registration of functional bean definitions[/-][+]Provide additional conditional registration capabilities in `BeanRegistrar`[/+]OlgaMaciaszek commentedon Mar 21, 2025
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.
DanielLiu1123 commentedon Sep 17, 2025
+1
jhoeller commentedon Mar 28, 2026
I've introduced a
DeferredBeanRegistrarvariant for 7.1, along with variouscontainsBeanmethods onBeanRegistry.The
containsBeanmethods are just able to expose an intermediate registration state with a regularBeanRegistrarwhich is to be invoked immediately at the time of its registration. However, they will provide reliable answers with aDeferredBeanRegistrarwhich is to be invoked at the end of the registration phase, independent of the order of user bean registration versusBeanRegistrarimport/registration. This allows forDeferredBeanRegistrarimplementations with customcontainsBeanchecks inifstatements, seeing all user-registered beans at that point, reacting to the overall state of the user configuration.Introduce DeferredBeanRegistrar and BeanRegistry#containsBean methods
Replace DeferredBeanRegistrar with implicit ordering semantics
jhoeller commentedon Apr 1, 2026
Based on feedback from the Boot team, I've replaced
DeferredBeanRegistrarwith 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.[-]Provide additional conditional registration capabilities in `BeanRegistrar`[/-][+]Provide bean conditional registration capabilities in `BeanRegistrar`[/+]Adapt bean overriding test for deferred BeanRegistrar processing in 7.1