quarkusio/quarkus · error · DefinitionException

Producer method must not have a @Disposes parameter (alterna

Error message

Producer method must not have a @Disposes parameter (alternatively, disposer method must not be annotated @Produces): 

What it means

ArC throws this DefinitionException when a producer method has a parameter annotated @Disposes, or a disposer method is annotated @Produces. Producer methods take only plain injection parameters; disposal belongs on a separate @Disposes method that consumes the produced object. forBean's METHOD branch checks producer parameters for DISPOSES.

Source

Thrown at independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/Injection.java:282

                }
                if (Annotations.hasParameterAnnotation(beanDeployment, initializerMethod, DotNames.OBSERVES_ASYNC)) {
                    throw new DefinitionException("Initializer method must not have an @ObservesAsync parameter "
                            + "(alternatively, async observer method must not be annotated @Inject): "
                            + beanTarget.asClass() + "." + initializerMethod.name());
                }
            }
            validateInjections(injections, beanType);
            return injections;
        } else if (Kind.METHOD.equals(beanTarget.kind())) {
            MethodInfo producerMethod = beanTarget.asMethod();

            if (beanDeployment.hasAnnotation(producerMethod, DotNames.INJECT)) {
                throw new DefinitionException("Producer method must not be annotated @Inject "
                        + "(alternatively, initializer method must not be annotated @Produces): "
                        + producerMethod);
            }
            if (Annotations.hasParameterAnnotation(beanDeployment, producerMethod, DotNames.DISPOSES)) {
                throw new DefinitionException("Producer method must not have a @Disposes parameter "
                        + "(alternatively, disposer method must not be annotated @Produces): "
                        + producerMethod);
            }
            if (Annotations.hasParameterAnnotation(beanDeployment, producerMethod, DotNames.OBSERVES)) {
                throw new DefinitionException("Producer method must not have an @Observes parameter "
                        + "(alternatively, observer method must not be annotated @Produces): "
                        + producerMethod);
            }
            if (Annotations.hasParameterAnnotation(beanDeployment, producerMethod, DotNames.OBSERVES_ASYNC)) {
                throw new DefinitionException("Producer method must not have an @ObservesAsync parameter "
                        + "(alternatively, async observer method must not be annotated @Produces): "
                        + producerMethod);
            }

            if (producerMethod.parameterTypes().isEmpty()) {
                return Collections.emptyList();
            }
            // All parameters are injection points

View on GitHub (pinned to e1c734241f)

Solutions

  1. Remove @Disposes from the producer method parameter
  2. Add a separate disposer method in the same bean: void dispose(@Disposes Resource r) { r.close(); }
  3. Verify the disposer is in the same class (or a dependent bean) as the producer so ArC can pair them
  4. Rebuild; check runs at augmentation time

Example fix

// before
@Produces
Resource produce(@Disposes Config cfg) { return new Resource(cfg); }

// after
@Produces
Resource produce(Config cfg) { return new Resource(cfg); }

void dispose(@Disposes Resource r) { r.close(); }
Defensive patterns

Strategy: validation

Validate before calling

void checkProducer(Method m) {
    if (!m.isAnnotationPresent(jakarta.enterprise.inject.Produces.class)) return;
    for (var p : m.getParameters()) {
        if (p.isAnnotationPresent(jakarta.enterprise.inject.Disposes.class)) {
            throw new IllegalStateException("@Disposes on producer param: " + m);
        }
    }
}

Prevention

When it happens

Trigger: Declaring @Produces Resource produce(@Disposes Resource r); detected by Annotations.hasParameterAnnotation(producerMethod, DotNames.DISPOSES) in forBean.

Common situations: Trying to define production and disposal in one method; copy-pasting a disposer signature into the producer; misunderstanding the producer/disposer pairing model in CDI.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/05ccd1f188bdeeda. Report an issue: GitHub.