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
a @Configuration
class.
@Configuration
@Import(MyBeanRegistrar.class)
class MyConfiguration {
}
Can also be applied to an application context via
GenericApplicationContext.register(BeanRegistrar...).
Bean registrar implementations use 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!")));
}
}
}
A BeanRegistrar implementing ImportAware
can optionally introspect import metadata when used in an import scenario, otherwise the
setImportMetadata method is simply not being called.
In Kotlin, it is recommended to use BeanRegistrarDsl instead of
implementing BeanRegistrar.
- Since:
- 7.0
- Author:
- Sebastien Deleuze
-
Method Summary
Modifier and TypeMethodDescriptionvoidregister(BeanRegistry registry, Environment env) Register beans on the givenBeanRegistryin a programmatic way.
-
Method Details
-
register
Register beans on the givenBeanRegistryin 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
-