{"id":"deeed9b0bcb3ead5","repo":"spring-projects/spring-framework","slug":"cannot-invoke-instance-method-without-factorybeann","errorCode":null,"errorMessage":"Cannot invoke instance method without factoryBeanName: {}","messagePattern":"Cannot invoke instance method without factoryBeanName: (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanInstanceSupplier.java","lineNumber":361,"sourceCode":"\t}\n\n\tprivate Object instantiate(RegisteredBean registeredBean, Executable executable, @Nullable Object[] args) {\n\t\tif (executable instanceof Constructor<?> constructor) {\n\t\t\tif (registeredBean.getBeanFactory() instanceof DefaultListableBeanFactory dlbf &&\n\t\t\t\t\tregisteredBean.getMergedBeanDefinition().hasMethodOverrides()) {\n\t\t\t\treturn dlbf.getInstantiationStrategy().instantiate(registeredBean.getMergedBeanDefinition(),\n\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","sourceCodeStart":343,"sourceCodeEnd":379,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanInstanceSupplier.java#L343-L379","documentation":"When BeanInstanceSupplier.instantiate invokes a factory method that is an instance (non-static) method, it needs a factory-bean instance to call it on. The factory bean name is taken from the merged bean definition's factoryBeanName; if that is null for a non-static method, Spring cannot determine the receiver and throws IllegalStateException.","triggerScenarios":"A bean defined with an instance factory-method but no factory-bean (or factoryBeanName resolved to null), encountered during AOT instance-supplier execution at runtime.","commonSituations":"Programmatic bean registration that sets a factory method via setFactoryMethodName on a non-static method but forgets setFactoryBeanName; XML config with factory-method but a missing or misspelled factory-bean attribute; refactoring that made a static factory method non-static without adding a factory-bean.","solutions":["Set the factory-bean/factoryBeanName on the bean definition to the bean that owns the instance factory method.","Alternatively make the factory method static so no receiver is needed.","If using programmatic BeanDefinitionBuilder, call .factoryBean(\"factoryBeanName\").factoryMethod(\"method\")."],"exampleFix":"// before\nBeanDefinitionBuilder.rootBeanDefinition(Svc.class).factoryMethod(\"create\"); // create() is non-static\n// after\nBeanDefinitionBuilder.rootBeanDefinition(Svc.class)\n  .factoryBean(\"svcFactory\").factoryMethod(\"create\");","handlingStrategy":"validation","validationCode":"RootBeanDefinition bd = registeredBean.getMergedBeanDefinition();\nif (bd.getFactoryMethodName() != null) {\n  Method m = findFactoryMethod(bd);\n  if (!Modifier.isStatic(m.getModifiers()) && bd.getFactoryBeanName() == null) {\n    throw new IllegalStateException(\"Instance factory method needs factoryBeanName: \" + m);\n  }\n}","typeGuard":"boolean factoryMethodResolvable(RootBeanDefinition bd, Method m) {\n  return Modifier.isStatic(m.getModifiers()) || bd.getFactoryBeanName() != null;\n}","tryCatchPattern":null,"preventionTips":["Always pair a non-static factory-method with a factory-bean attribute.","Prefer static factory methods when there is no natural factory bean.","Add a config validation test that checks every factory-method bean has a resolvable receiver."],"tags":["spring","aot","factory-method","configuration"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}