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.
BeanRegistry and Environment to register beans:
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!")));
}
}
}
BeanRegistrar implementations are not Spring components: they must have
a no-arg constructor and cannot rely on dependency injection or any other
component-model feature. They can be used in two distinct ways depending on the
application context setup.
With the @Configuration model
A BeanRegistrar must be imported via
@Import on a
@Configuration class:
@Configuration
@Import(MyBeanRegistrar.class)
class MyConfiguration {
}
This is the only mechanism that triggers bean registration in the annotation-based
configuration model. Annotating an implementation with @Configuration or
@Component, or returning an instance from a @Bean method, registers
it as a bean but does not invoke its
register method.
When imported, the registrar is invoked in the order it is encountered during configuration class processing. It can therefore check for and build on beans that have already been defined, but has no visibility into beans that will be registered by classes processed later.
Programmatic usage
A BeanRegistrar can also be applied directly to a
GenericApplicationContext:
GenericApplicationContext context = new GenericApplicationContext();
context.register(new MyBeanRegistrar());
context.registerBean("myBean", MyBean.class);
context.refresh();
This mode is primarily intended for fully programmatic application context setups.
Registrars applied this way are invoked before any @Configuration class is
processed. They can therefore observe beans registered programmatically (e.g., via
one of the GenericApplicationContext#registerBean methods), but will
not see any beans defined in @Configuration classes also
registered with the context.
A BeanRegistrar implementing ImportAware
can optionally introspect import metadata when used in an import scenario; otherwise
the setImportMetadata method is not 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
-