apache/dubbo · error · ScopeBeanException

already exists bean with same name and type, name=${name}, t

Error message

already exists bean with same name and type, name=${name}, type=${className}

What it means

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.

Source

Thrown at dubbo-common/src/main/java/org/apache/dubbo/common/beans/factory/ScopeBeanFactory.java:119

    public <T> void registerBeanFactory(Supplier<T> factory) {
        registerBeanFactory(null, factory);
    }

    @SuppressWarnings("unchecked")
    public <T> void registerBeanFactory(String name, Supplier<T> factory) {
        Class<T> clazz = (Class<T>) TypeUtils.getSuperGenericType(factory.getClass(), 0);
        if (clazz == null) {
            throw new ScopeBeanException("unable to determine bean class from factory's superclass or interface");
        }
        registeredBeanDefinitions.add(new BeanDefinition<>(name, clazz, factory));
    }

    private <T> T createAndRegisterBean(String name, Class<T> clazz) {
        checkDestroyed();
        T instance = getBean(name, clazz);
        if (instance != null) {
            throw new ScopeBeanException(
                    "already exists bean with same name and type, name=" + name + ", type=" + clazz.getName());
        }
        try {
            instance = instantiationStrategy.instantiate(clazz);
        } catch (Throwable e) {
            throw new ScopeBeanException("create bean instance failed, type=" + clazz.getName(), e);
        }
        registerBean(name, instance);
        return instance;
    }

    public void registerBean(Object bean) {
        registerBean(null, bean);
    }

    public void registerBean(String name, Object bean) {
        checkDestroyed();
        // avoid duplicated register same bean

View on GitHub (pinned to 3a3043227f)

Solutions

  1. Guard registration with getBean(name, type) == null (or containsBean) before calling registerBean, and skip if present.
  2. Remove the duplicate registration call site identified by the name/type in the message.
  3. Use a unique bean name for the second registration if both beans are genuinely distinct.
  4. Ensure custom SPI extensions or post-processors do not re-register beans Dubbo already created.

Example fix

// before
factory.registerBean("cache", CacheBean.class);
factory.registerBean("cache", CacheBean.class);  // duplicate
// after
if (factory.getBean("cache", CacheBean.class) == null) {
    factory.registerBean("cache", CacheBean.class);
}
Defensive patterns

Strategy: validation

Validate before calling

// Check for an existing bean before (re)registering
boolean isAbsent(ScopeBeanFactory f, String name, Class<?> type) {
    return !f.containsBean(name, null) && f.getBean(name, type) == null;
}
// if (isAbsent(f, name, type)) f.registerBean(name, type);

Try / catch

try {
    f.registerBean(name, type);
} catch (ScopeBeanException e) {
    if (e.getMessage().startsWith("already exists bean with same name and type")) {
        // bean already present; optionally fetch it: f.getBean(name, type)
    } else throw e;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of apache/dubbo@3a3043227f (2026-08-14). Data as JSON: /api/errors/62a12c13c3775c1e. Report an issue: GitHub.