ConditionalOnMissingBean class

Registers a bean only when no such bean is registered yet: the way a library offers a default that the application can replace.

This is the most used condition of auto-configuration. A starter declares its Clock, HttpClient or ObjectMapper with it; when the application declares its own bean of that type, the starter's bean is not registered and the application's is injected everywhere. Because auto-configuration is registered after the application's beans, the application's bean is always seen.

What must be missing: a bean of type or of any of types, named name or any of names, or carrying one of annotations. Without any of them, a bean of the annotated bean's own type (the @Bean method's return type). Beans of ignored types do not count; search limits the contexts searched. As with ConditionalOnBean, beans declared with autowireCandidate: false or defaultCandidate: false do not count when searching by type.

@AutoConfiguration()
class HttpAutoConfiguration {
  @Bean()
  @ConditionalOnMissingBean()
  HttpClientSettings httpSettings() => HttpClientSettings();

  @Bean()
  @ConditionalOnMissingBean.where(ignored: [TestClock])
  Clock clock() => SystemClock();
}

Spring (Java): @ConditionalOnMissingBean, @ConditionalOnMissingBean(ignored = TestClock.class).

Implemented types

Constructors

ConditionalOnMissingBean([Type? type])
Requires that no bean of type exists; without type, no bean of the annotated bean's own type.
const
ConditionalOnMissingBean.named(String name, {SearchStrategy search = SearchStrategy.all})
Requires that no bean named name exists in the contexts search selects (Spring Boot's @ConditionalOnMissingBean(name = "...")).
const
ConditionalOnMissingBean.where({List<Type> types = const [], List<String> names = const [], List<Type> annotations = const [], List<Type> ignored = const [], SearchStrategy search = SearchStrategy.all})
Requires that none of the beans described exists: none of types, none named in names, none carrying one of annotations. When all three are empty, no bean of the annotated bean's own type. Beans of the ignored types do not count.
const

Properties

annotations → List<Type>
Annotation classes no bean may carry on its class or @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
ignored → List<Type>
Bean types that do not count, for example a test double that should not make the real bean back off.
final
name → String?
A name no bean may have, given to ConditionalOnMissingBean.named.
final
names → List<String>
Names no bean may have, given to ConditionalOnMissingBean.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 no bean may have, given to the unnamed constructor.
final
types → List<Type>
Types no bean may have, given to ConditionalOnMissingBean.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