ConditionalOnProperty class

Registers a bean only when a property is set, or set to a given value.

It is how a feature is switched on or off from configuration, without code changes: put it on a @Component() class, a @Bean method or a configuration class. A starter uses it so that an application can turn one of its beans off with a property.

The property is name, or each of names (all must match), each after prefix (a . is added when missing). Without havingValue any value except false matches; with it, the value must equal it, ignoring case. A property that is not set does not match unless matchIfMissing is true. For an enabled flag, ConditionalOnBooleanProperty says the same more plainly.

// Search is on unless feature.search=false.
@Service()
@ConditionalOnProperty('feature.search', matchIfMissing: true)
class SearchService {}

@Bean()
@ConditionalOnProperty.names(['a', 'b'], prefix: 'app.feature')
Feature feature() => Feature(); // app.feature.a and app.feature.b

Spring (Java):

@ConditionalOnProperty(name = "feature.search", matchIfMissing = true)
@ConditionalOnProperty(prefix = "app.feature", name = {"a", "b"})
Implemented types

Constructors

ConditionalOnProperty(String name, {String prefix = '', String? havingValue, bool matchIfMissing = false})
Checks the property name (after prefix). By default it matches when the property is set to anything but false.
const
ConditionalOnProperty.names(List<String> names, {String prefix = '', String? havingValue, bool matchIfMissing = false})
Checks every property of names (each after prefix); all of them must match.
const

Properties

hashCode → int
The hash code for this object.
no setterinherited
havingValue → String?
The value the property must have, compared ignoring case. null (or empty) accepts any value except false.
final
matchIfMissing → bool
Whether a property that is not set matches. False by default, so a feature guarded this way is off until switched on.
final
name → String?
The property checked, when the unnamed constructor was used; null with ConditionalOnProperty.names.
final
names → List<String>
The properties checked with ConditionalOnProperty.names; empty with the unnamed constructor.
final
prefix → String
Put before each name, so prefix: 'app' with 'enabled' checks app.enabled. Empty by default.
final
propertyNames → List<String>
The full names of the checked properties, with prefix applied.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

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