{"record":{"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/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/aot/DefaultBeanRegistrationCodeFragments.java#L64-L100","documentation":"Thrown as an AotBeanProcessingException with 'instance supplier is not supported' when DefaultBeanRegistrationCodeFragments.getTarget is called for a bean whose merged bean definition has a non-null InstanceSupplier. AOT codegen cannot statically reproduce an arbitrary programmatic instance supplier, so it refuses to compute the target class for the bean.","triggerScenarios":"A bean definition has been given an InstanceSupplier via setInstanceSupplier(...), and AOT processing reaches getTarget(registeredBean) (e.g. to determine the generated target class for the bean registration). Triggered on beans that were registered with a programmatic supplier rather than a resolvable constructor or factory method.","commonSituations":"Calling context.registerBean(MyBean.class, () -> ...) or setInstanceSupplier(...) and then running AOT/native-image; Spring Boot auto-configuration in a version that registers an instance supplier; migrating a context to native without refactoring dynamic registrations; testing utilities that supply instances programmatically.","solutions":["Refactor the bean to a class Spring can instantiate via constructor or factory method (remove the InstanceSupplier).","If you must keep dynamic creation, exclude the bean from AOT processing or use BeanFactory instrumentation at runtime rather than registration time.","Move the dynamic logic inside a @Bean method that returns a concrete type Spring can analyze.","Register an explicit RootBeanDefinition with setBeanClass/setFactoryMethodName instead of an instance supplier.","Upgrade Spring: some suppliers added by older Boot versions are AOT-incompatible; newer Boot avoids them."],"exampleFix":"// before: programmatic instance supplier that breaks AOT\ncontext.registerBean(MyBean.class, () -> new MyBean(secret()));\n\n// after: plain @Bean method Spring can AOT-process\n@Configuration\nclass Cfg {\n    @Bean MyBean myBean() { return new MyBean(secret()); }\n}","handlingStrategy":"validation","validationCode":"// Fail fast if a bean has an instance supplier and AOT is enabled\nRootBeanDefinition mbd = (RootBeanDefinition) registeredBean.getMergedBeanDefinition();\nif (mbd.getInstanceSupplier() != null) {\n    throw new IllegalStateException(\n        \"Bean '\" + registeredBean.getBeanName() + \"' uses an instance supplier; AOT is not possible.\");\n}","typeGuard":"// True if the bean definition is AOT-compatible\nboolean aotCompatible = mbd.getInstanceSupplier() == null;","tryCatchPattern":null,"preventionTips":["Avoid context.registerBean(Class, Supplier) when targeting native image.","Prefer declarative @Bean/@Configuration factory methods.","Audit BeanFactoryPostProcessors that assign instance suppliers.","Upgrade Spring Boot to a version whose auto-configurations are AOT-clean."],"tags":["spring","aot","native-image","instance-supplier","code-generation"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}