{"record":{"id":"d23851dc96d5fde8","repo":"apache/dubbo","slug":"create-bean-instance-failed-type-classname","errorCode":null,"errorMessage":"create bean instance failed, type=${className}","messagePattern":"create bean instance failed, type=(.+?)","errorType":"exception","errorClass":"ScopeBeanException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/beans/factory/ScopeBeanFactory.java","lineNumber":125,"sourceCode":"    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\n        if (containsBean(name, bean)) {\n            return;\n        }\n\n        Class<?> beanClass = bean.getClass();\n        if (name == null) {","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/beans/factory/ScopeBeanFactory.java#L107-L143","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the chained cause (ScopeBeanException.getCause()) to find the real reflective/construction failure.","Add a public no-arg constructor, or a single constructor taking ScopeModel/ApplicationModel/ModuleModel, to the target class.","Ensure the class is concrete and its constructor does not fail during initialization (move heavy logic to an init() / Initializable method).","On JDK 17+, add the necessary opens/exports to module-info or --add-opens so reflection can reach the constructor."],"exampleFix":"// before\npublic class MyBean {\n    public MyBean(DataSource ds) { ... }  // no matched/no default constructor\n}\nfactory.registerBean(\"myBean\", MyBean.class);\n// after\npublic class MyBean {\n    public MyBean() { ... }  // default constructor\n}\nfactory.registerBean(\"myBean\", MyBean.class);","handlingStrategy":"try-catch","validationCode":"// Sanity-check a class is instantiable by InstantiationStrategy before registering\nboolean isReflectivelyInstantiable(Class<?> c) {\n    if (Modifier.isAbstract(c.getModifiers()) || c.isInterface()) return false;\n    try { c.getConstructor(); return true; } catch (NoSuchMethodException ignored) {}\n    for (Constructor<?> ctor : c.getConstructors()) {\n        boolean ok = true;\n        for (Class<?> p : ctor.getParameterTypes())\n            ok &= org.apache.dubbo.rpc.model.ScopeModel.class.isAssignableFrom(p);\n        if (ok) return true;\n    }\n    return false;\n}","typeGuard":null,"tryCatchPattern":"try {\n    factory.registerBean(name, type);\n} catch (ScopeBeanException e) {\n    Throwable cause = e.getCause();\n    // cause is the ReflectiveOperationException from instantiate(); inspect and fix constructor\n    log.error(\"Cannot create bean {} ({})\", type, cause);\n}","preventionTips":["Always inspect getCause() of a bean-creation ScopeBeanException.","Keep a public no-arg or single ScopeModel constructor on registerable beans.","Keep constructors lightweight; move heavy work to Initializable.initialize().","On JDK 17+, ensure modules open the relevant package for reflection."],"tags":["bean-factory","instantiation","reflection","dubbo-common"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}