Interface BeanRegistrar
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Contract for registering beans programmatically.
Typically imported with an @Import
annotation on @Configuration
classes.
@Configuration @Import(MyBeanRegistrar.class) class MyConfiguration { }
The bean registrar implementation uses BeanRegistry
and Environment
APIs to register beans programmatically in a concise and flexible way.
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!"))); } } }
In Kotlin, it is recommended to use BeanRegistrarDsl
instead of
implementing BeanRegistrar
.
- Since:
- 7.0
- Author:
- Sebastien Deleuze
-
Method Summary
Modifier and TypeMethodDescriptionvoid
register
(BeanRegistry registry, Environment env) Register beans on the givenBeanRegistry
in a programmatic way.
-
Method Details
-
register
Register beans on the givenBeanRegistry
in a programmatic way.- Parameters:
registry
- the bean registry to operate onenv
- the environment that can be used to get the active profile or some properties
-