{"record":{"id":"759cc7d1f6556652","repo":"apache/maven","slug":"cannot-rebind-scope-annotation-class-to-a-differen","errorCode":null,"errorMessage":"Cannot rebind scope annotation class to a different implementation: {}","messagePattern":"Cannot rebind scope annotation class to a different implementation: (.+?)","errorType":"exception","errorClass":"DIException","httpStatus":null,"severity":"error","filePath":"impl/maven-di/src/main/java/org/apache/maven/di/impl/InjectorImpl.java","lineNumber":129,"sourceCode":"                }\n            }\n        } catch (Exception e) {\n            throw new DIException(\"Error while discovering DI classes from classLoader\", e);\n        }\n        return this;\n    }\n\n    @Nonnull\n    @Override\n    public Injector bindScope(@Nonnull Class<? extends Annotation> scopeAnnotation, @Nonnull Scope scope) {\n        return bindScope(scopeAnnotation, () -> scope);\n    }\n\n    @Nonnull\n    @Override\n    public Injector bindScope(@Nonnull Class<? extends Annotation> scopeAnnotation, @Nonnull Supplier<Scope> scope) {\n        if (scopes.put(scopeAnnotation, scope) != null) {\n            throw new DIException(\n                    \"Cannot rebind scope annotation class to a different implementation: \" + scopeAnnotation);\n        }\n        return this;\n    }\n\n    @Nonnull\n    @Override\n    public <U> Injector bindInstance(@Nonnull Class<U> clazz, @Nonnull U instance) {\n        Key<?> key = Key.of(clazz, ReflectionUtils.qualifierOf(clazz));\n        Binding<U> binding = Binding.toInstance(instance);\n        return doBind(key, binding);\n    }\n\n    @Override\n    public <U> Injector bindSupplier(@Nonnull Class<U> clazz, @Nonnull Supplier<U> supplier) {\n        Key<?> key = Key.of(clazz, ReflectionUtils.qualifierOf(clazz));\n        Binding<U> binding = Binding.toSupplier(supplier);\n        return doBind(key, binding);","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/impl/maven-di/src/main/java/org/apache/maven/di/impl/InjectorImpl.java#L111-L147","documentation":"bindScope associates a scope annotation (Singleton, per-lookup, custom) with its Scope implementation. Scopes live in a map keyed by annotation class; if the annotation was already registered, scopes.put returns the previous entry and the injector throws DIException. Despite the message saying 'different implementation', any second bindScope for the same annotation fails.","triggerScenarios":"Two bindScope calls with the same annotation class on one injector — e.g. two extensions or bootstrap modules each registering @Singleton, or re-running configuration against a shared injector.","commonSituations":"Adding a custom scope that a core module already binds; plexus-to-DI migrations double-registering scopes; tests that reuse a global injector across different configurations.","solutions":["Locate the duplicate: the message names the annotation class — search every bindScope call for it","Bind each scope annotation exactly once per injector and centralize scope setup in one bootstrap place","Create a fresh injector per application or test instead of re-configuring a shared one"],"exampleFix":"// before\ninjector.bindScope(Singleton.class, singletonScope); // second registration -> DIException\n\n// after\nSet<Class<? extends Annotation>> registered = new HashSet<>();\nif (registered.add(Singleton.class)) {\n    injector.bindScope(Singleton.class, singletonScope);\n}","handlingStrategy":"validation","validationCode":"private final Set<Class<? extends Annotation>> boundScopes = new HashSet<>();\n\nvoid bindScopeOnce(Injector injector, Class<? extends Annotation> anno, Scope scope) {\n    if (boundScopes.add(anno)) {\n        injector.bindScope(anno, scope);\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    injector.bindScope(Singleton.class, scope);\n} catch (DIException e) {\n    // annotation already registered: skip or reuse the existing scope registration\n    log.debug(\"scope already bound: {}\", e.getMessage());\n}","preventionTips":["Centralize bindScope calls in a single bootstrap class","Use a fresh injector per context rather than mutating a long-lived one"],"tags":["di","scope","duplicate-binding","maven"],"backgroundTag":"duplicate-di-binding","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}