This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Framework 6.2.12!

Programmatic Bean Registration

As of Spring Framework 7, a first-class support for programmatic bean registration is provided via the BeanRegistrar interface that can be implemented to register beans programmatically in a flexible and efficient way.

Those bean registrar implementations are typically imported with an @Import annotation on @Configuration classes.

  • Java

  • Kotlin

@Configuration
@Import(MyBeanRegistrar.class)
class MyConfiguration {
}
@Configuration
@Import(MyBeanRegistrar::class)
class MyConfiguration {
}
You can leverage type-level conditional annotations (@Conditional, but also other variants) to conditionally import the related bean registrars.

The bean registrar implementation uses BeanRegistry and Environment APIs to register beans programmatically in a concise and flexible way. For example, it allows custom registration through an if expression, a for loop, etc.

  • Java

  • Kotlin

class MyBeanRegistrar implements BeanRegistrar {

	@Override
	public void register(BeanRegistry registry, Environment env) {
		registry.registerBean("foo", Foo.class);
		registry.registerBean("bar", Bar.class, spec -> spec
				.prototype()
				.lazyInit()
				.description("Custom description")
				.supplier(context -> new Bar(context.bean(Foo.class))));
		if (env.matchesProfiles("baz")) {
			registry.registerBean(Baz.class, spec -> spec
					.supplier(context -> new Baz("Hello World!")));
		}
		registry.registerBean(MyRepository.class);
		registry.registerBean(RouterFunction.class, spec ->
				spec.supplier(context -> router(context.bean(MyRepository.class))));
	}

	RouterFunction<ServerResponse> router(MyRepository myRepository) {
		return RouterFunctions.route()
				// ...
				.build();
	}

}
class MyBeanRegistrar : BeanRegistrarDsl({
	registerBean<Foo>()
	registerBean(
		name = "bar",
		prototype = true,
		lazyInit = true,
		description = "Custom description") {
		Bar(bean<Foo>()) // Also possible with Bar(bean())
	}
	profile("baz") {
		registerBean { Baz("Hello World!") }
	}
	registerBean<MyRepository>()
	registerBean {
		myRouter(bean<MyRepository>()) // Also possible with myRouter(bean())
	}
})

fun myRouter(myRepository: MyRepository) = router {
	// ...
}
Bean registrars are supported with Ahead of Time Optimizations, either on the JVM or with GraalVM native images, including when instance suppliers are used.