registerBean<T extends Object> method
void
registerBean<T extends Object>(
- T supplier(
- DependencyResolver context
- String? name,
- List<
Type> ? types, - String scope = ScopeType.singleton,
- bool lazy = false,
- bool primary = false,
- bool fallback = false,
- bool autowireCandidate = true,
- bool defaultCandidate = true,
- int? order,
- String? description,
- List<
String> aliases = const [], - List<
String> dependsOn = const [],
Adds a bean of type T, created by supplier, before refresh.
Use it for a bean that is not in a module: an object built from
command-line arguments, or a test replacement marked primary. The
supplier gets a resolver for the bean's own dependencies. The
options are those of BeanRegistry.registerBean; without a name
the bean is called Type#0, Type#1, ... The bean is registered while
the modules load, in order with the registered components. Throws a
StateError once refresh has started.
// A system clock, and a mailer that takes precedence over the
// module's one.
final context = AnnotationConfigApplicationContext([appBeans])
..registerBean<Clock>((_) => SystemClock(), name: 'clock')
..registerBean<Mailer>(
(r) => SmtpMailer(r.dependency<Clock>()),
primary: true,
)
..refresh();
Spring (Java):
var context = new AnnotationConfigApplicationContext();
context.registerBean("clock", Clock.class, SystemClock::new);
context.registerBean(Mailer.class,
() -> new SmtpMailer(context.getBean(Clock.class)),
definition -> definition.setPrimary(true));
context.refresh();
Implementation
void registerBean<T extends Object>(
T Function(DependencyResolver context) supplier, {
String? name,
List<Type>? types,
String scope = ScopeType.singleton,
bool lazy = false,
bool primary = false,
bool fallback = false,
bool autowireCandidate = true,
bool defaultCandidate = true,
int? order,
String? description,
List<String> aliases = const [],
List<String> dependsOn = const [],
}) {
_checkNotRefreshed("register bean '${name ?? T}'");
_components.add(
_SingleBeanRegistrar(
(registry) => registry.registerBean<T>(
supplier,
name: name,
types: types,
scope: scope,
lazy: lazy,
primary: primary,
fallback: fallback,
autowireCandidate: autowireCandidate,
defaultCandidate: defaultCandidate,
order: order,
description: description,
aliases: aliases,
dependsOn: dependsOn,
),
),
);
}