apache/dubbo · error · ScopeBeanException

create bean instance failed, type=${className}

Error message

create bean instance failed, type=${className}

What it means

Thrown by ScopeBeanFactory.createAndRegisterBean when instantiationStrategy.instantiate(clazz) throws any Throwable while creating a bean on demand. The original exception is chained as the cause. This wraps construction failures (missing/ambiguous constructors, reflective access errors, constructor exceptions) into a single ScopeBeanException so callers see a consistent bean-creation error.

Source

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

    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
        if (containsBean(name, bean)) {
            return;
        }

        Class<?> beanClass = bean.getClass();
        if (name == null) {

View on GitHub (pinned to 3a3043227f)

Solutions

  1. Read the chained cause (ScopeBeanException.getCause()) to find the real reflective/construction failure.
  2. Add a public no-arg constructor, or a single constructor taking ScopeModel/ApplicationModel/ModuleModel, to the target class.
  3. Ensure the class is concrete and its constructor does not fail during initialization (move heavy logic to an init() / Initializable method).
  4. On JDK 17+, add the necessary opens/exports to module-info or --add-opens so reflection can reach the constructor.

Example fix

// before
public class MyBean {
    public MyBean(DataSource ds) { ... }  // no matched/no default constructor
}
factory.registerBean("myBean", MyBean.class);
// after
public class MyBean {
    public MyBean() { ... }  // default constructor
}
factory.registerBean("myBean", MyBean.class);
Defensive patterns

Strategy: try-catch

Validate before calling

// Sanity-check a class is instantiable by InstantiationStrategy before registering
boolean isReflectivelyInstantiable(Class<?> c) {
    if (Modifier.isAbstract(c.getModifiers()) || c.isInterface()) return false;
    try { c.getConstructor(); return true; } catch (NoSuchMethodException ignored) {}
    for (Constructor<?> ctor : c.getConstructors()) {
        boolean ok = true;
        for (Class<?> p : ctor.getParameterTypes())
            ok &= org.apache.dubbo.rpc.model.ScopeModel.class.isAssignableFrom(p);
        if (ok) return true;
    }
    return false;
}

Try / catch

try {
    factory.registerBean(name, type);
} catch (ScopeBeanException e) {
    Throwable cause = e.getCause();
    // cause is the ReflectiveOperationException from instantiate(); inspect and fix constructor
    log.error("Cannot create bean {} ({})", type, cause);
}

Prevention

When it happens

Trigger: registerBean(name, Class) for a class that InstantiationStrategy cannot build: no default constructor and no constructor taking ScopeModel subclasses, an abstract class/interface, a constructor that throws, or reflective access blocked. See InstantiationStrategy.instantiate for the matching rules.

Common situations: Registering a class with no usable constructor; constructor that performs work failing at startup (DB/filesystem/env not ready); JDK 17+ module access denial to a non-public constructor; passing an interface or abstract class to registerBean.

Related errors


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