-
Notifications
You must be signed in to change notification settings - Fork 38.4k
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
Provide additional conditional registration capabilities in BeanRegistrar
#21497
Comments
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 As things stand, condition evaluation is tied to the 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. |
Spring Framework 7 introduces a new We should explore if more is needed or if that's flexible enough. |
@dsyer @rwinch @wilkinsona @philwebb I am now targeting Spring Framework For example, we could provide an equivalent of That does not mean we will add support for all current annotation conditions support at Spring Boot level. For example, for now we don't necessarily plan to add support for an equivalent of The most challenging limitation we will have to handle is that |
I'm not sure that Perhaps something like
|
My mistake, I meant |
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 @Configuration
@Import(MySecurity.class)
class MyConfiguration {
}
class MySecurity extends SecurityRegistrar {
@Override
public void register(HttpSecurity registry, Environment env) {
http.formLogin();
}
}
// Could be provided by Spring Security
abstract class SecurityRegistrar 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 For classpath checks, it is unclear if we should let users just use if statements with 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. |
BeanRegistrar
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. |
Dave Syer opened SPR-16959 and commented
There is a gap in the the functional bean registration features in
BeanDefinitionBuilder
andGenericApplicationContext
. Once you register aSupplier
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 theBeanDefinitionRegistry
interface, for example to support aPredicate<ConditionContext>
as well as theSupplier
. If the interface doesn't change, I suppose returningnull
from theSupplier
might be an option, but that seems a bit ugly, and might be too late, since theBeanDefinition
has already been registered at that point.Issue Links:
The text was updated successfully, but these errors were encountered: