{"record":{"id":"62a12c13c3775c1e","repo":"apache/dubbo","slug":"already-exists-bean-with-same-name-and-type-name","errorCode":null,"errorMessage":"already exists bean with same name and type, name=${name}, type=${className}","messagePattern":"already exists bean with same name and type, name=(.+?), type=(.+?)","errorType":"exception","errorClass":"ScopeBeanException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/beans/factory/ScopeBeanFactory.java","lineNumber":119,"sourceCode":"\n    public <T> void registerBeanFactory(Supplier<T> factory) {\n        registerBeanFactory(null, factory);\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    public <T> void registerBeanFactory(String name, Supplier<T> factory) {\n        Class<T> clazz = (Class<T>) TypeUtils.getSuperGenericType(factory.getClass(), 0);\n        if (clazz == null) {\n            throw new ScopeBeanException(\"unable to determine bean class from factory's superclass or interface\");\n        }\n        registeredBeanDefinitions.add(new BeanDefinition<>(name, clazz, factory));\n    }\n\n    private <T> T createAndRegisterBean(String name, Class<T> clazz) {\n        checkDestroyed();\n        T instance = getBean(name, clazz);\n        if (instance != null) {\n            throw new ScopeBeanException(\n                    \"already exists bean with same name and type, name=\" + name + \", type=\" + clazz.getName());\n        }\n        try {\n            instance = instantiationStrategy.instantiate(clazz);\n        } catch (Throwable e) {\n            throw new ScopeBeanException(\"create bean instance failed, type=\" + clazz.getName(), e);\n        }\n        registerBean(name, instance);\n        return instance;\n    }\n\n    public void registerBean(Object bean) {\n        registerBean(null, bean);\n    }\n\n    public void registerBean(String name, Object bean) {\n        checkDestroyed();\n        // avoid duplicated register same bean","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/beans/factory/ScopeBeanFactory.java#L101-L137","documentation":"Thrown by ScopeBeanFactory.createAndRegisterBean when a bean with the same name AND type already exists in the scope. Before instantiating, it calls getBean(name, clazz); a non-null result means the registration would collide, which Dubbo treats as a configuration error rather than silently overwriting.","triggerScenarios":"Calling registerBean(name, SomeClass.class) twice with the same name/type, or two extension/bean definitions resolving to the same name+type within one ScopeBeanFactory. Also when getOrRegisterBean is invoked after an equivalent bean was already registered (e.g. double initialization, duplicate SPI extension).","commonSituations":"Duplicate @DubboService or manual registrations of the same bean; custom ExtensionPostProcessor that re-registers a bean already created by Dubbo; running initialization logic twice (e.g. after a context refresh); two modules sharing a ScopeBeanFactory both registering the same component.","solutions":["Guard registration with getBean(name, type) == null (or containsBean) before calling registerBean, and skip if present.","Remove the duplicate registration call site identified by the name/type in the message.","Use a unique bean name for the second registration if both beans are genuinely distinct.","Ensure custom SPI extensions or post-processors do not re-register beans Dubbo already created."],"exampleFix":"// before\nfactory.registerBean(\"cache\", CacheBean.class);\nfactory.registerBean(\"cache\", CacheBean.class);  // duplicate\n// after\nif (factory.getBean(\"cache\", CacheBean.class) == null) {\n    factory.registerBean(\"cache\", CacheBean.class);\n}","handlingStrategy":"validation","validationCode":"// Check for an existing bean before (re)registering\nboolean isAbsent(ScopeBeanFactory f, String name, Class<?> type) {\n    return !f.containsBean(name, null) && f.getBean(name, type) == null;\n}\n// if (isAbsent(f, name, type)) f.registerBean(name, type);","typeGuard":null,"tryCatchPattern":"try {\n    f.registerBean(name, type);\n} catch (ScopeBeanException e) {\n    if (e.getMessage().startsWith(\"already exists bean with same name and type\")) {\n        // bean already present; optionally fetch it: f.getBean(name, type)\n    } else throw e;\n}","preventionTips":["Guard registrations with getBean(name, type) == null before calling registerBean.","Make custom ExtensionPostProcessors idempotent so they never re-register an existing bean.","Avoid double initialization of modules that share a ScopeBeanFactory.","Use unique bean names for genuinely distinct beans."],"tags":["bean-factory","duplicate-registration","configuration","dubbo-common"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}