Bean class
Marks a method whose return value is a bean.
Use it when a class cannot carry Component itself (it comes from
another package, or needs constructor arguments that are not beans), or
when creating the object takes code. The method lives in a
Configuration class (or a @Component, in "lite" mode) or is static;
its parameters are injected like constructor parameters.
The bean name defaults to the method name. The bean type is what the
method returns: the static type of the returned expression when it is
more specific than the declared return type, so
DataSource dataSource() => HikariDataSource() is registered as a
HikariDataSource and all its supertypes (Spring only knows the
declared type until the bean is created). A method declared T? may
return null: the bean then exists by name but is absent for lookups
and injection by type. A function type or record type can be a bean too,
injected by that exact type.
@Configuration()
class AppConfig {
// The pool size comes from the configuration, 4 when unset; open()
// runs once the pool is created.
@Bean(name: 'mainPool', aliases: ['pool'], initMethod: 'open')
Pool pool(@Value(r'${pool.size:4}') int size) => Pool(size);
@Bean(destroyMethod: '') // do not call Client.close() on shutdown
static Client client() => Client();
}
Spring (Java):
@Configuration
class AppConfig {
@Bean(name = {"mainPool", "pool"}, initMethod = "open")
Pool pool(@Value("${pool.size:4}") int size) { return new Pool(size); }
@Bean(destroyMethod = "")
static Client client() { return new Client(); }
}
Constructors
Properties
-
aliases
→ List<
String> -
More names the bean can be found under. Spring puts them in the
namearray after the first.final - autowireCandidate → bool
-
Whether the bean can be injected by type at all (also with a
qualifier). With
falseit is still found by name.final - defaultCandidate → bool
-
Whether the bean is injected where only its type is asked for. With
falseit is injected only where a qualifier (@Qualifier, the bean name or an alias) selects it, and@ConditionalOnBean/@ConditionalOnMissingBeanignore it. Use it for a second bean of a common type that must not get in the way of the main one (Spring 6.2's@Bean(defaultCandidate = false)).final - destroyMethod → String?
-
A no-argument method of the bean called when the context closes.
Defaults to inferMethod; an empty string calls nothing, for an
object whose
close()must not run (for example one shared with code outside the container).final - hashCode → int
-
The hash code for this object.
no setterinherited
- initMethod → String?
-
A no-argument method of the bean called once it is injected, for
objects that need a start call (
open,connect) and that you cannot annotate with@PostConstruct.final - name → String?
-
The bean name;
nullgives the method name.final - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Constants
- inferMethod → const String
-
The default of destroyMethod: at shutdown, call a public
no-argument
close(),shutdown()ordispose()method of the declared return type, if it has one. Spring inferscloseandshutdown;disposeis the Dart idiom.