ConditionalOnSingleCandidate class

Registers a bean only when exactly one bean of a type can be injected where that type is asked for.

ConditionalOnBean is satisfied by several beans of a type, but a bean that takes one of them as a constructor parameter would then fail with an ambiguity. This condition checks that the injection works: there is one candidate, or one @Primary() among several, or one that is not @Fallback(). Beans with autowireCandidate: false or defaultCandidate: false do not count; beans of parent contexts do. Like ConditionalOnBean, it only sees beans registered before it.

// Safe to inject a DataSource here: there is exactly one to choose.
@Bean()
@ConditionalOnSingleCandidate(DataSource)
Migrations migrations(DataSource source) => Migrations(source);

Spring (Java): @ConditionalOnSingleCandidate(DataSource.class).

Implemented types

Constructors

ConditionalOnSingleCandidate(Type type)
Requires that a single bean of type can be injected.
const

Properties

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
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
type → Type
The type that must have a single injectable bean.
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