quarkusio/quarkus · error · DefinitionException
Disposer method must not be annotated @Inject (alternatively
Error message
Disposer method must not be annotated @Inject (alternatively, initializer method must not have a @Disposes parameter):
What it means
Build-time CDI definition error from Injection.forDisposer: the method recognized as a disposer (it has a @Disposes parameter) is itself annotated @Inject, i.e. the same method is both an initializer and a disposer. The CDI spec forbids this combination; the offending method is appended to the message.
Source
Thrown at independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/Injection.java:404
if (!classInfo.superName().equals(DotNames.OBJECT)) {
ClassInfo info = getClassByName(beanDeployment.getBeanArchiveIndex(), classInfo.superName());
if (info != null) {
List<Injection> superInjections = forClassBean(beanClass, info, beanDeployment, transformer, true, seenMethods);
// injections are discovered bottom-up to easily skip overriden methods,
// but they need to be performed top-down per the AtInject specification
superInjections.addAll(injections);
injections = superInjections;
}
}
return injections;
}
static Injection forDisposer(MethodInfo disposerMethod, ClassInfo beanClass, BeanDeployment beanDeployment,
InjectionPointModifier transformer) {
if (beanDeployment.hasAnnotation(disposerMethod, DotNames.INJECT)) {
throw new DefinitionException("Disposer method must not be annotated @Inject "
+ "(alternatively, initializer method must not have a @Disposes parameter): "
+ disposerMethod);
}
if (beanDeployment.hasAnnotation(disposerMethod, DotNames.PRODUCES)) {
throw new DefinitionException("Disposer method must not be annotated @Produces "
+ "(alternatively, producer method must not have a @Disposes parameter): "
+ disposerMethod);
}
if (Annotations.hasParameterAnnotation(beanDeployment, disposerMethod, DotNames.OBSERVES)) {
throw new DefinitionException("Disposer method must not have an @Observes parameter "
+ "(alternatively, observer method must not have a @Disposes parameter): "
+ disposerMethod);
}
if (Annotations.hasParameterAnnotation(beanDeployment, disposerMethod, DotNames.OBSERVES_ASYNC)) {
throw new DefinitionException("Disposer method must not have an @ObservesAsync parameter "
+ "(alternatively, async observer method must not have a @Disposes parameter): "
+ disposerMethod);
}View on GitHub (pinned to e1c734241f)
Solutions
- Remove @Inject from the disposer method; its non-@Disposes parameters are resolved as injection points automatically
- Move initialization logic into a separate @Inject initializer method
- Drop the @Disposes parameter if the method was meant to be a plain initializer
Example fix
// before
@Inject
void dispose(@Disposes Widget w) {}
// after
void dispose(@Disposes Widget w) {} Defensive patterns
Strategy: validation
Validate before calling
for (Method m : MyClass.class.getDeclaredMethods()) {
boolean hasDisposes = java.util.Arrays.stream(m.getParameterAnnotations())
.flatMap(java.util.Arrays::stream).anyMatch(a -> a.annotationType().getSimpleName().equals("Disposes"));
if (hasDisposes && m.isAnnotationPresent(Inject.class)) {
throw new IllegalStateException("@Inject disposer: " + m);
}
} Prevention
- Never combine @Inject with a @Disposes parameter
- Give each lifecycle role (inject/produce/dispose/observe) its own method
When it happens
Trigger: Annotating a @Disposes-parameter method with @Inject; detected in Injection.forDisposer at build time.
Common situations: Adding @Inject to a disposer thinking it enables injection into its other parameters; combining producer/disposer/injector roles on one method after refactoring.
Related errors
- Multiple disposed parameters found for ${disposerMethod}
- Initializer method must not be annotated @Produces (alternat
- Producer method must not be annotated @Inject (alternatively
- Disposer method must not be annotated @Produces (alternative
- Disposer method must not have an @Observes parameter (altern
AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05).
Data as JSON: /api/errors/ec241f244c77e3ee.
Report an issue: GitHub.