Spring / Spring7 basics Interview questions
What is the BeanRegistrar contract introduced in Spring Framework 7?
BeanRegistrar is a new programmatic bean-registration contract added in Spring Framework 7 that gives developers a cleaner, code-first alternative to cramming complex registration logic into @Bean methods. Spring's own guidance discourages registering several beans from a single @Bean method or returning a broad supertype, since that limits flexibility - BeanRegistrar exists precisely for the cases those rules get in the way of.
class MetricsRegistrar implements BeanRegistrar { @Override public void register(BeanRegistry registry, Environment env) { registry.registerBean("meterRegistry", MeterRegistry.class, spec -> spec.supplier(ctx -> new SimpleMeterRegistry())); } }
It's especially useful for conditional or dynamic bean registration, such as registering a different set of beans depending on active profiles or discovered configuration.
More Related questions...