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
typeexists; withouttype, no bean of the annotated bean's own type.const - ConditionalOnMissingBean.named(String name, {SearchStrategy search = SearchStrategy.all})
-
Requires that no bean named
nameexists in the contextssearchselects (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 innames, none carrying one ofannotations. When all three are empty, no bean of the annotated bean's own type. Beans of theignoredtypes do not count.const
Properties
-
annotations
→ List<
Type> -
Annotation classes no bean may carry on its class or
@Beanmethod; 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
OnBeanConditionis aREGISTER_BEANConfigurationCondition).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
- search → SearchStrategy
-
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;
contextgives 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