{"record":{"id":"9198e4f61d29a128","repo":"spring-projects/spring-framework","slug":"failed-to-instantiate-method","errorCode":null,"errorMessage":"Failed to instantiate method","messagePattern":"Failed to instantiate method","errorType":"exception","errorClass":"BeanInstantiationException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanInstanceSupplier.java","lineNumber":368,"sourceCode":"\t\t\t\t\t\tregisteredBean.getBeanName(), registeredBean.getBeanFactory());\n\t\t\t}\n\t\t\treturn BeanUtils.instantiateClass(constructor, args);\n\t\t}\n\t\tif (executable instanceof Method method) {\n\t\t\tObject target = null;\n\t\t\tString factoryBeanName = registeredBean.getMergedBeanDefinition().getFactoryBeanName();\n\t\t\tif (factoryBeanName != null) {\n\t\t\t\ttarget = registeredBean.getBeanFactory().getBean(factoryBeanName, method.getDeclaringClass());\n\t\t\t}\n\t\t\telse if (!Modifier.isStatic(method.getModifiers())) {\n\t\t\t\tthrow new IllegalStateException(\"Cannot invoke instance method without factoryBeanName: \" + method);\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tReflectionUtils.makeAccessible(method);\n\t\t\t\treturn method.invoke(target, args);\n\t\t\t}\n\t\t\tcatch (Throwable ex) {\n\t\t\t\tthrow new BeanInstantiationException(method, ex.getMessage(), ex);\n\t\t\t}\n\t\t}\n\t\tthrow new IllegalStateException(\"Unsupported executable \" + executable.getClass().getName());\n\t}\n\n\n\tprivate static String toCommaSeparatedNames(Class<?>... parameterTypes) {\n\t\treturn Arrays.stream(parameterTypes).map(Class::getName).collect(Collectors.joining(\", \"));\n\t}\n\n\n\t/**\n\t * Performs lookup of the {@link Executable}.\n\t */\n\tabstract static class ExecutableLookup {\n\n\t\tabstract Executable get(RegisteredBean registeredBean);\n\t}","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanInstanceSupplier.java#L350-L386","documentation":"Thrown as a BeanInstantiationException when invoking the constructor or factory method of a bean via reflection throws any Throwable. BeanInstanceSupplier.instantiate wraps the underlying cause (IllegalAccessException, InvocationTargetException, error inside the constructor, etc.) into a BeanInstantiationException so a single, consistent exception type surfaces from bean creation.","triggerScenarios":"method.invoke(target, args) (or BeanUtils.instantiateClass) throws because the constructor/factory method itself raised an exception, the JVM denied access, the bean threw in its constructor, an argument type mismatch caused a runtime exception, or a static initializer failed.","commonSituations":"Bean constructor throws NullPointerException/IllegalArgumentException because of bad state; required property missing because bean is being instantiated before its @PostConstruct-style init; native-image missing reflection registration causing IllegalAccess; constructor argument types changed between compile and AOT metadata; third-party library throwing in its constructor (driver init, connection pool).","solutions":["Read the 'caused by' of the BeanInstantiationException to see the real exception thrown by the constructor and fix it at the source.","If access fails, ensure the constructor/factory method is registered for reflection (ExecutableMode.INVOKE) in native image, typically via the AOT runtime hints.","Confirm the constructor argument types/count match what the supplier is passing (signature drift after refactor).","Initialize required state before bean creation (provide constructor args, properties, or environment values) so the constructor does not throw.","Unit-test the constructor/factory method in isolation to reproduce the failure without Spring."],"exampleFix":"// before: constructor throws because a required arg is null\npublic MyService(Dependency dep) {\n    this.dep = Objects.requireNonNull(dep); // NPE here\n}\n\n// after: ensure the dependency is provided/registered before instantiation\n@Configuration\nclass Cfg {\n    @Bean Dependency dep() { return new DefaultDependency(); }\n    @Bean MyService myService(Dependency dep) { return new MyService(dep); }\n}","handlingStrategy":"try-catch","validationCode":"// Smoke-test the constructor in isolation before Spring invokes it\nMyBean b = new MyBean(requiredArg1, requiredArg2);\nAssert.notNull(b, \"bean must construct\");","typeGuard":null,"tryCatchPattern":"try {\n    return beanFactory.getBean(MyBean.class);\n} catch (BeanInstantiationException ex) {\n    Throwable cause = ex.getCause();\n    throw new IllegalStateException(\"Constructor of \" + ex.getBeanClass() + \" failed\", cause);\n}","preventionTips":["Unit-test bean constructors with realistic inputs before wiring them in Spring.","Register constructors for reflection (ExecutableMode.INVOKE) in native image.","Validate required properties before construction (Objects.requireNonNull in the constructor).","Keep constructor argument types stable across builds to avoid AOT drift."],"tags":["spring","aot","bean-instantiation","reflection","native-image"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}