Conditional class

Registers a bean only when every listed Condition matches.

Use it when the rule is not a profile: an operating system, a property combination, the presence of another bean. Each type in value must implement Condition and have a no-argument constructor; the container calls its matches while it loads the bean definitions, before any bean is created. A const annotation class that implements Condition can be used directly instead (this is how Spring Boot's @ConditionalOnProperty works), and a subclass of Conditional names a set of conditions once.

class OnLinux implements Condition {
  const OnLinux();

  @override
  bool matches(ConditionContext context) =>
      context.environment.getProperty('os.name') == 'linux';
}

// Only on Linux machines, where systemd exists.
@Component()
@Conditional([OnLinux])
class SystemdNotifier { ... }

Spring (Java):

class OnLinux implements Condition {
  public boolean matches(ConditionContext ctx, AnnotatedTypeMetadata m) {
    return "linux".equals(ctx.getEnvironment().getProperty("os.name"));
  }
}

@Component
@Conditional(OnLinux.class)
class SystemdNotifier { ... }

Constructors

Conditional(List<Type> value)
Restricts the bean to the Condition types value.
const

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
value → List<Type>
Condition implementations that all have to match.
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

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