quarkusio/quarkus · error · DefinitionException

Producer method must not be annotated @Inject (alternatively

Error message

Producer method must not be annotated @Inject (alternatively, initializer method must not be annotated @Produces): 

What it means

ArC throws this DefinitionException when a producer method (@Produces) is also annotated @Inject — the mirror case of error 2763. A method cannot be both a producer and an injection target; when beanTarget.kind() is METHOD, forBean checks the producer method for DotNames.INJECT and rejects the definition.

Source

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

                }
                if (Annotations.hasParameterAnnotation(beanDeployment, initializerMethod, DotNames.OBSERVES)) {
                    throw new DefinitionException("Initializer method must not have an @Observes parameter "
                            + "(alternatively, observer method must not be annotated @Inject): "
                            + beanTarget.asClass() + "." + initializerMethod.name());
                }
                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);
            }

View on GitHub (pinned to e1c734241f)

Solutions

  1. Remove @Inject from the producer method, keeping @Produces
  2. If the method was meant as an initializer, remove @Produces instead and keep @Inject with void return semantics
  3. Ensure the class containing the producer is itself a bean (@ApplicationScoped etc.) if you expected it to be injectable
  4. Rebuild; error is raised during augmentation

Example fix

// before
@Inject
@Produces
Config config() { return loadConfig(); }

// after
@Produces
Config config() { return loadConfig(); }
Defensive patterns

Strategy: validation

Validate before calling

void checkMethod(Method m) {
    boolean inject = m.isAnnotationPresent(jakarta.inject.Inject.class);
    boolean produces = m.isAnnotationPresent(jakarta.enterprise.inject.Produces.class);
    if (inject && produces) {
        throw new IllegalStateException("@Inject + @Produces on: " + m);
    }
}

Prevention

When it happens

Trigger: Annotating a @Produces method with @Inject in a bean class; detected in forBean's METHOD branch via beanDeployment.hasAnnotation(producerMethod, DotNames.INJECT).

Common situations: IDE quick-fixes adding @Inject alongside @Produces; assuming producer methods need @Inject to participate in injection; code merged from two sources each adding one annotation.

Related errors


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