Configuration class
Marks a class whose Bean methods create beans.
Use it for objects you cannot annotate yourself (a class from another
package, a connection built from properties) or that need setup code:
each @Bean method returns one bean, and its parameters are injected.
The class itself is a bean too.
By default (proxyBeanMethods true) the container creates a
generated subclass whose @Bean methods return the container's bean.
So when one @Bean method calls another, it gets the existing
singleton, not a second object (a prototype-scoped @Bean still gives
a new instance per call). Static @Bean methods are not intercepted.
Because of the subclass, a class with instance @Bean methods must be
extendable (not final, sealed or interface) and have a public
generative constructor; otherwise the build fails. With
proxyBeanMethods false ("lite" mode) the methods run as written:
take other beans as method parameters instead of calling the methods.
@Lazy, @Profile and conditions on the class apply to all its
@Bean methods, unless a method says otherwise (@Lazy(false)).
@Configuration()
class DataConfig {
// Opened with the URL from the configuration files.
@Bean(initMethod: 'connect')
Connection connection(@Value(r'${db.url}') String url) =>
Connection(url);
@Bean()
UserStore userStore(Connection connection) => UserStore(connection);
@Bean()
Clock clock() => SystemClock();
@Bean()
Scheduler scheduler() => Scheduler(clock()); // the clock singleton
}
@Configuration(proxyBeanMethods: false)
class LiteConfig {
@Bean()
Clock clock() => SystemClock();
}
Spring (Java), where the subclass is made with CGLIB:
@Configuration
class DataConfig {
@Bean(initMethod = "connect")
Connection connection(@Value("${db.url}") String url) {
return new Connection(url);
}
@Bean
UserStore userStore(Connection connection) {
return new UserStore(connection);
}
@Bean Clock clock() { return new SystemClock(); }
@Bean Scheduler scheduler() { return new Scheduler(clock()); }
}
@Configuration(proxyBeanMethods = false)
class LiteConfig {
@Bean Clock clock() { return new SystemClock(); }
}
Constructors
- Configuration({String? name, bool proxyBeanMethods = true})
-
Declares the configuration class;
namereplaces the default bean name.const
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- name → String?
-
The bean name;
nullgives the class name in lowerCamelCase.finalinherited - proxyBeanMethods → bool
-
Whether a generated subclass makes
@Beanmethods return the container's beans (defaulttrue). Withfalsethe methods are called as written, which avoids the subclass and its restrictions.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