Skip to content

Conditional Bean Activation in CDI #986

Description

@otaviojava

Is your feature request related to a problem? Please describe.

Enterprise applications often require enabling or disabling beans according to deployment-specific conditions.

Examples include:

  • Activating a tax calculation implementation based on country.
  • Enabling integrations only in specific environments.
  • Switching between providers based on configuration.
  • Enabling feature-flagged functionality.

Today, CDI applications typically solve these scenarios using producer methods, extensions, or custom framework-specific solutions:

@Produces
public TaxCalculator taxCalculator(Config config) {
    return "BR".equals(config.country())
            ? new BrazilTaxCalculator()
            : new PortugalTaxCalculator();
}

Although effective, this approach adds boilerplate and separates activation logic from the bean definition.

Describe the solution you'd like

Introduce a standard CDI mechanism to support conditional bean activation.

For example:

@Conditional(CountryCondition.class)
@ApplicationScoped
public class BrazilTaxCalculator implements TaxCalculator {
}

This approach would make bean activation declarative, enhance readability, and reduce reliance on producer-based workarounds.

Describe alternatives you've considered

Currently, similar functionality can be achieved using:

  • Producer methods
  • Portable Extensions
  • Build Compatible Extensions
  • Custom framework-specific solutions

Other frameworks offer similar capabilities:

  • Apache DeltaSpike: @Exclude
  • Spring: @Conditional
  • Spring: @Profile
  • Spring Boot: @ConditionalOnProperty

Example:

@Conditional(MyCondition.class)
@Component
class MyService {
}

These examples demonstrate that conditional bean activation is a common requirement across dependency injection frameworks.

Additional context

A standard solution would simplify enterprise use cases such as multi-country deployments, feature flags, environment-specific integrations, and provider selection.

It would also offer a portable alternative to custom extensions and framework-specific annotations, while keeping activation logic close to the bean definition.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions