ConditionalOnBean class

Registers a bean only when some other bean is already registered.

A starter uses it for beans that build on something the application may or may not have: register database migrations only when there is a DataSource, a metrics exporter only when there is a registry.

The required beans: one of type and of every one of types, one named name and every one of names, and one annotated with each of annotations (the exact annotation class, on the bean's class or @Bean method). Without any of them, the bean's own type is required (the @Bean method's return type). search limits the contexts searched.

The condition is decided when the bean definitions are registered, configuration class by configuration class, so it only sees what was registered before. That is why it belongs on auto-configuration, which comes after the application's beans; on an application class it depends on the order of the classes. As in Spring Boot 3.4+, beans declared with autowireCandidate: false or defaultCandidate: false do not count when searching by type (for ConditionalOnMissingBean either); beans of parent contexts do.

// Migrations only when the application has a DataSource.
@Bean()
@ConditionalOnBean(DataSource)
Migrations migrations(DataSource source) => Migrations(source);

// Both beans must be in this context, not in a parent.
@Bean()
@ConditionalOnBean.where(
  types: [DataSource, Clock],
  search: SearchStrategy.current,
)
Audit audit() => Audit();

Spring (Java): @ConditionalOnBean(DataSource.class), @ConditionalOnBean(value = {DataSource.class, Clock.class}, search = SearchStrategy.CURRENT).

Implemented types

Constructors

ConditionalOnBean([Type? type])
Requires a bean of type; without type, a bean of the annotated bean's own type.
const
ConditionalOnBean.named(String name, {SearchStrategy search = SearchStrategy.all})
Requires a bean named name, in the contexts search selects (Spring Boot's @ConditionalOnBean(name = "...")).
const
ConditionalOnBean.where({List<Type> types = const [], List<String> names = const [], List<Type> annotations = const [], SearchStrategy search = SearchStrategy.all})
Requires every bean described: one of each of types, each of names, and one carrying each of annotations. When all three are empty, a bean of the annotated bean's own type is required.
const

Properties

annotations → List<Type>
Annotation classes that must each be on some bean (its class or its @Bean method); the exact class counts, not a subclass.
final
configurationPhase → ConfigurationPhase
Decided when the bean definitions are registered, after every earlier configuration class, so that it sees their beans (Spring Boot's OnBeanCondition is a REGISTER_BEAN ConfigurationCondition).
no setteroverride
hashCode → int
The hash code for this object.
no setterinherited
name → String?
A name a bean must have, given to ConditionalOnBean.named.
final
names → List<String>
Names that must each be present, given to ConditionalOnBean.where.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
The contexts searched; SearchStrategy.all by default.
final
type → Type?
A type a bean must have, given to the unnamed constructor.
final
types → List<Type>
Types that must each be present, given to ConditionalOnBean.where.
final

Methods

matches(ConditionContext context) → bool
Whether the annotated bean should be registered; context gives access to the configuration and the definitions registered so far.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
override

Operators

operator ==(Object other) → bool
The equality operator.
inherited