{"id":"e0a49ff020f2e255","repo":"spring-projects/spring-framework","slug":"instance-supplier-is-not-supported","errorCode":null,"errorMessage":"instance supplier is not supported","messagePattern":"instance supplier is not supported","errorType":"exception","errorClass":"AotBeanProcessingException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/aot/DefaultBeanRegistrationCodeFragments.java","lineNumber":82,"sourceCode":"\n\tprivate final Supplier<InstantiationDescriptor> instantiationDescriptor;\n\n\n\tDefaultBeanRegistrationCodeFragments(\n\t\t\tBeanRegistrationsCode beanRegistrationsCode, RegisteredBean registeredBean,\n\t\t\tBeanDefinitionMethodGeneratorFactory beanDefinitionMethodGeneratorFactory) {\n\n\t\tthis.beanRegistrationsCode = beanRegistrationsCode;\n\t\tthis.registeredBean = registeredBean;\n\t\tthis.beanDefinitionMethodGeneratorFactory = beanDefinitionMethodGeneratorFactory;\n\t\tthis.instantiationDescriptor = SingletonSupplier.of(registeredBean::resolveInstantiationDescriptor);\n\t}\n\n\n\t@Override\n\tpublic ClassName getTarget(RegisteredBean registeredBean) {\n\t\tif (hasInstanceSupplier()) {\n\t\t\tthrow new AotBeanProcessingException(registeredBean, \"instance supplier is not supported\");\n\t\t}\n\t\tClass<?> target = extractDeclaringClass(registeredBean, this.instantiationDescriptor.get());\n\t\twhile (target.getName().startsWith(\"java.\") && registeredBean.isInnerBean()) {\n\t\t\tRegisteredBean parent = registeredBean.getParent();\n\t\t\tAssert.state(parent != null, \"No parent available for inner bean\");\n\t\t\ttarget = parent.getBeanClass();\n\t\t}\n\t\treturn (target.isArray() ? ClassName.get(target.getComponentType()) : ClassName.get(target));\n\t}\n\n\tprivate Class<?> extractDeclaringClass(RegisteredBean registeredBean, InstantiationDescriptor instantiationDescriptor) {\n\t\tClass<?> declaringClass = ClassUtils.getUserClass(instantiationDescriptor.targetClass());\n\t\tif (instantiationDescriptor.executable() instanceof Constructor<?> ctor &&\n\t\t\t\tAccessControl.forMember(ctor).isPublic() && FactoryBean.class.isAssignableFrom(declaringClass)) {\n\t\t\treturn extractTargetClassFromFactoryBean(declaringClass, registeredBean.getBeanType());\n\t\t}\n\t\treturn declaringClass;\n\t}","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-beans/src/main/java/org/springframework/beans/factory/aot/DefaultBeanRegistrationCodeFragments.java#L64-L100","documentation":"DefaultBeanRegistrationCodeFragments.getTarget refuses to compute the target class for a bean whose definition carries an instance supplier (setInstanceSupplier). AOT cannot serialize an arbitrary programmatic Supplier to source code, so any bean with an instance supplier is unsupported for native/AOT. Thrown as AotBeanProcessingException.","triggerScenarios":"A bean registered programmatically via BeanDefinition.setInstanceSupplier(...) (or GenericApplicationContext.registerBean with a Supplier) is processed by the Spring AOT engine during getTarget.","commonSituations":"Spring Boot native-image build failing on beans registered with registerBean(Supplier) or setInstanceSupplier; functional bean registration (context.registerBean(MyType.class, () -> ...)) in a native-targeted app.","solutions":["Replace the instance supplier with a constructor-based or static factory-method bean definition that AOT can code-generate.","If you must keep the supplier, exclude the bean from native/AOT processing or run in JIT mode.","Refactor to register the bean via @Configuration/@Bean so AOT has a statically analyzable creation path."],"exampleFix":"// before\ncontext.registerBean(MyBean.class, () -> new MyBean(config));\n// after\n@Configuration\nclass Cfg {\n  @Bean MyBean myBean() { return new MyBean(cfg); }\n}","handlingStrategy":"validation","validationCode":"for (String name : context.getBeanDefinitionNames()) {\n  BeanDefinition bd = context.getBeanFactory().getBeanDefinition(name);\n  if (bd.getInstanceSupplier() != null) {\n    log.warn(\"Bean {} uses an instance supplier -> unsupported by AOT\", name);\n  }\n}","typeGuard":"boolean isAotCompatible(BeanDefinition bd) {\n  return bd.getInstanceSupplier() == null;\n}","tryCatchPattern":null,"preventionTips":["Avoid registerBean(Supplier)/setInstanceSupplier for native targets.","Express beans via @Bean/@Configuration for AOT analyzability.","Add a CI check that flags instance-supplier beans when native is enabled."],"tags":["spring","aot","native","instance-supplier"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}