quarkusio/quarkus · error · DefinitionException

Injected field cannot be annotated with @Produces: ${field}

Error message

Injected field cannot be annotated with @Produces: ${field}

What it means

A field annotated @Inject (an injected field, i.e. managed by the container) cannot also be annotated @Produces. Producer fields must not be injection targets; ArC throws this DefinitionException during field scanning of the bean class.

Source

Thrown at independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanDeployment.java:1305

                                            "Observer method found but %s has no bean defining annotation - using @Dependent",
                                            beanClass);
                                }
                            }
                        }
                    }
                }
                DotName superType = aClass.superName();
                aClass = superType != null && !superType.equals(DotNames.OBJECT)
                        ? getClassByName(getBeanArchiveIndex(), superType)
                        : null;
            }

            // non-inherited fields
            for (FieldInfo field : beanClass.fields()) {
                if (annotationStore.hasAnnotation(field, DotNames.PRODUCES)
                        && !annotationStore.hasAnnotation(field, DotNames.VETOED_PRODUCER)) {
                    if (annotationStore.hasAnnotation(field, DotNames.INJECT)) {
                        throw new DefinitionException("Injected field cannot be annotated with @Produces: " + field);
                    }
                    // Do not register classes with producers and no bean def. annotation as beans in strict mode
                    // Producer fields are not inherited
                    if (strictCompatibility) {
                        if (hasBeanDefiningAnnotation) {
                            producerFields.add(field);
                        }
                    } else {
                        producerFields.add(field);
                        if (!hasBeanDefiningAnnotation) {
                            LOGGER.debugf("Producer field found but %s has no bean defining annotation - using @Dependent",
                                    beanClass);
                            beanClasses.add(beanClass);
                        }
                    }
                } else if (!annotationStore.hasAnnotation(field, DotNames.PRODUCES)) {
                    // Verify that non-producer fields are not annotated with stereotypes
                    // (vetoed producers must _not_ be checked)

View on GitHub (pinned to e1c734241f)

Solutions

  1. Remove @Inject from the producer field and inject the needed dependencies elsewhere (e.g. constructor).
  2. If you actually want injection, remove @Produces instead.
  3. If the field must keep both for compatibility but should not be registered, mark it @VetoedProducer (or @Vetoed) to skip producer registration.

Example fix

// before
@Inject
@Produces
Config config;
// after
@Produces
Config config; // dependencies injected via constructor instead
Defensive patterns

Strategy: validation

Validate before calling

if (field.isAnnotationPresent(Inject.class) && field.isAnnotationPresent(Produces.class)) {
    throw new IllegalStateException("@Inject and @Produces cannot both be on " + field);
}

Prevention

When it happens

Trigger: A field has both @Inject and @Produces annotations (and is not a @VetoedProducer); BeanDeployment.deploy iterates beanClass.fields() and detects the combination.

Common situations: Converting an injected field into a producer by adding @Produces without removing @Inject; copy-paste of annotation blocks; IDE quick-fixes stacking annotations.

Related errors


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