{"record":{"id":"fd5cbf10174014ec","repo":"quarkusio/quarkus","slug":"multiple-beans-match-the-type","errorCode":null,"errorMessage":"Multiple beans match the type: ","messagePattern":"Multiple beans match the type: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"extensions/scheduler/common/src/main/java/io/quarkus/scheduler/common/runtime/util/SchedulerUtils.java","lineNumber":119,"sourceCode":"        if (!value.isEmpty() && isConfigValue(value)) {\n            value = resolvePropertyExpression(adjustExpressionSyntax(value));\n        }\n        return value;\n    }\n\n    public static boolean isConfigValue(String val) {\n        return isSimpleConfigValue(val) || isConfigExpression(val);\n    }\n\n    public static ZoneId parseCronTimeZone(Scheduled scheduled) {\n        String timeZone = lookUpPropertyValue(scheduled.timeZone());\n        return timeZone.equals(Scheduled.DEFAULT_TIMEZONE) ? null : ZoneId.of(timeZone);\n    }\n\n    public static <T> T instantiateBeanOrClass(Class<T> type) {\n        Instance<T> instance = Arc.container().select(type, Any.Literal.INSTANCE);\n        if (instance.isAmbiguous()) {\n            throw new IllegalArgumentException(\"Multiple beans match the type: \" + type);\n        } else if (instance.isUnsatisfied()) {\n            try {\n                return type.getConstructor().newInstance();\n            } catch (Exception e) {\n                throw new IllegalStateException(\"Unable to instantiate the class: \" + type);\n            }\n        } else {\n            return instance.get();\n        }\n    }\n\n    private static boolean isSimpleConfigValue(String val) {\n        val = val.trim();\n        return val.startsWith(\"{\") && val.endsWith(\"}\");\n    }\n\n    /**\n     * Converts \"{property}\" to \"${property}\" for backwards compatibility","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/scheduler/common/src/main/java/io/quarkus/scheduler/common/runtime/util/SchedulerUtils.java#L101-L137","documentation":"SchedulerUtils.instantiateBeanOrClass() resolves a task/skip-predicate Class either as a CDI bean (via Arc.container().select) or, if no bean exists, by calling its no-arg constructor. When multiple CDI beans match the requested type with @Any qualifier, the resolution is ambiguous and this IllegalArgumentException is thrown (note: the companion IllegalStateException covers reflection failures).","triggerScenarios":"Passing a Class to setTask/setAsyncTask/setSkipPredicate (or calling instantiateBeanOrClass directly) where two or more beans of that type exist in the CDI container, e.g. a bean plus a @Mock bean in tests, or multiple implementations/specializations of the same interface type.","commonSituations":"Test profiles adding a @Mock/@Alternative duplicate of a scheduled task bean; two beans of the same class in different packages both eligible; forgetting @Alternative/@Priority on a test replacement; accidentally making an abstract task class a bean.","solutions":["Disambiguate the container: mark one bean @Alternative with @Priority, or use @Named and select by qualifier","Remove or deactivate the duplicate bean (e.g. mock only active in the test profile without also keeping the production bean)","Make the duplicate non-bean (@Vetoed or remove @ApplicationScoped) so instantiation falls back to the constructor","Change the scheduled configuration to reference a unique concrete class"],"exampleFix":"// before\nclass MyTask {...} and @Mock class MyTaskMock extends MyTask {...} // both beans\n// after\n@Mock @Alternative @Priority(1)\nclass MyTaskMock extends MyTask {...} // ambiguity resolved","handlingStrategy":"validation","validationCode":"Instance<T> check = Arc.container().select(type, Any.Literal.INSTANCE);\nif (check.isAmbiguous()) {\n    throw new IllegalStateException(\"Multiple beans of \" + type + \" - add @Alternative/@Priority or @Vetoed\");\n}","typeGuard":"boolean uniquelyResolvable(Class<?> type) {\n    Instance<?> i = Arc.container().select(type, Any.Literal.INSTANCE);\n    return !i.isAmbiguous();\n}","tryCatchPattern":"try {\n    task = SchedulerUtils.instantiateBeanOrClass(MyTask.class);\n} catch (IllegalArgumentException e) {\n    LOG.error(\"Ambiguous CDI beans for {} - qualify or veto duplicates\", type, e);\n}","preventionTips":["Ensure exactly one bean per scheduled task/predicate type","Mark test mocks @Alternative with @Priority so they replace, not duplicate","Use @Vetoed on classes meant only for reflective instantiation","Keep task classes concrete and unique; avoid duplicate implementations across packages"],"tags":["scheduler","cdi","bean-resolution","ambiguity"],"backgroundTag":"ambiguous-bean-resolution","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}