OnAssetCondition class

A Condition implementation that determines whether an annotated component, configuration, or pod should be activated based on the presence of a specific asset within the application's resource path.

OnAssetCondition works together with the ConditionalOnAsset annotation to enable or disable configuration elements depending on whether an asset file (such as configuration JSON, YAML, or other resource) exists at the specified path.

This condition is particularly useful for auto-configuration systems that adapt behavior dynamically based on available files in the application bundle or runtime environment.

Behavior

  • If the annotated element includes a ConditionalOnAsset annotation:
    • The asset field defines the path of the asset that must exist.
    • The condition uses DefaultAssetPathResource to verify accessibility of the asset.
    • The condition passes if the asset exists and is retrievable.
    • If the asset is missing, the condition fails, preventing activation of the annotated element.
  • If no ConditionalOnAsset annotation is present, the condition passes automatically.

Example

// Activate configuration only if `config/app_config.json` exists
@ConditionalOnAsset('config/app_config.json')
class AppConfigPod {}

// Activate only when a credentials file is available
@ConditionalOnAsset('secrets/credentials.yaml')
class SecureConfig {}

Evaluation Flow

  1. When matches is invoked, the condition checks whether the Annotation has a ConditionalOnAsset annotation.
  2. If present, it extracts the declared asset path.
  3. It creates a DefaultAssetPathResource instance with that path and attempts to resolve it using DefaultAssetPathResource.tryGet.
  4. The condition passes (true) if the asset is found and accessible.
  5. If the asset cannot be located or read, the condition fails (false).
  6. If no annotation is detected, the condition passes by default.

Logging

When trace logging is enabled:

  • Logs the start of evaluation with the annotated element name.
  • Logs the asset path being evaluated.
  • Emits when the asset is found and accessible.
  • Emits when the asset is missing or cannot be read.
  • Logs all intermediate decisions to aid debugging in conditional configuration flows.
  • ConditionalOnAsset: Annotation declaring the asset path to check.
  • DefaultAssetPathResource: Utility for verifying the existence and accessibility of asset files.
  • ConditionalContext: Provides the runtime context and resource access for evaluation.
  • Source: Represents the annotated class or method being evaluated.
  • Condition: Base interface for all JetLeaf condition evaluators.
Implemented types

Constructors

OnAssetCondition()
A Condition implementation that determines whether an annotated component, configuration, or pod should be activated based on the presence of a specific asset within the application's resource path.
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

Methods

matches(ConditionalContext context, Annotation annotation, Source source) Future<bool>
Evaluates the condition against the given ConditionalContext and annotation for the specified Source.
override
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