{"record":{"id":"e0581ca8f229e84b","repo":"spring-projects/spring-framework","slug":"failed-to-generate-code-for-bean-definition","errorCode":null,"errorMessage":"failed to generate code for bean definition","messagePattern":"failed to generate code for bean definition","errorType":"exception","errorClass":"AotBeanProcessingException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContribution.java","lineNumber":280,"sourceCode":"\t\t\t});\n\t\t}\n\n\n\t\tprivate void generateRegisterBeanDefinitionMethods(MethodSpec.Builder method,\n\t\t\t\tIterable<Registration> registrations) {\n\n\t\t\tCodeBlock.Builder code = CodeBlock.builder();\n\t\t\tregistrations.forEach(registration -> {\n\t\t\t\ttry {\n\t\t\t\t\tCodeBlock methodInvocation = generateBeanRegistration(registration);\n\t\t\t\t\tcode.addStatement(\"$L.registerBeanDefinition($S, $L)\",\n\t\t\t\t\t\t\tBEAN_FACTORY_PARAMETER_NAME, registration.beanName(), methodInvocation);\n\t\t\t\t}\n\t\t\t\tcatch (AotException ex) {\n\t\t\t\t\tthrow ex;\n\t\t\t\t}\n\t\t\t\tcatch (Exception ex) {\n\t\t\t\t\tthrow new AotBeanProcessingException(registration.registeredBean,\n\t\t\t\t\t\t\t\"failed to generate code for bean definition\", ex);\n\t\t\t\t}\n\t\t\t});\n\t\t\tmethod.addCode(code.build());\n\t\t}\n\n\t\tprivate CodeBlock generateBeanRegistration(Registration registration) {\n\t\t\tMethodReference beanDefinitionMethod = registration.methodGenerator\n\t\t\t\t\t.generateBeanDefinitionMethod(this.generationContext, this.codeGenerator);\n\t\t\treturn beanDefinitionMethod.toInvokeCodeBlock(\n\t\t\t\t\tArgumentCodeGenerator.none(), this.codeGenerator.getClassName());\n\t\t}\n\t}\n\n}\n","sourceCodeStart":262,"sourceCodeEnd":296,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContribution.java#L262-L296","documentation":"Thrown as an AotBeanProcessingException when generating the code to register a bean definition fails for any reason other than an AotException. BeanRegistrationsAotContribution.generateRegisterBeanDefinitionMethods calls generateBeanRegistration per bean; any generic Exception during code generation (reflection lookup, JavaPoet build, value resolution) is wrapped with the registered bean and the message 'failed to generate code for bean definition' so you know which bean broke AOT processing.","triggerScenarios":"Any uncaught Exception (not subclass of AotException) thrown while BeanDefinitionMethodGenerator generates the bean-registration method for a single Registration during the AOT processing phase. Causes include unsupported bean definition shapes, missing classes, value codegen failures, and reflective metadata errors.","commonSituations":"Enabling Spring AOT / native-image on a context that contains a bean Spring cannot statically describe (lambda @Bean, programmatic instance supplier, dynamic bean definitions); using a third-party BeanDefinitionRegistryPostProcessor that adds unsupported metadata; upgrading Spring with stale generated sources; missing classes referenced only reflectively.","solutions":["Read the wrapped 'caused by' to find the real codegen error; the registeredBean in the exception names the offending bean.","Refactor the offending bean to a form Spring can AOT-process (concrete class or plain @Bean/@Configuration factory method, no lambda instance supplier).","Remove or exclude the bean from AOT processing if it is inherently dynamic.","Clean generated sources and regenerate (mvn/gradle clean) after fixing the underlying cause.","Align Spring module versions to eliminate codegen-path mismatches."],"exampleFix":"// before: lambda instance supplier Spring cannot AOT-generate\n@Bean\npublic MyBean myBean = () -> new MyBean(loadFromDb());\n\n// after: a @Bean method Spring can process at AOT time\n@Bean\npublic MyBean myBean(DataSource ds) {\n    return new MyBean(loadFromDb(ds));\n}","handlingStrategy":"try-catch","validationCode":"// Detect beans that AOT cannot handle before running the native build\nRootBeanDefinition mbd = (RootBeanDefinition) registeredBean.getMergedBeanDefinition();\nif (mbd.getInstanceSupplier() != null || mbd.getFactoryMethodName() == null && !mbd.hasBeanClass()) {\n    throw new IllegalStateException(\"Bean not AOT-friendly: \" + registeredBean.getBeanName());\n}","typeGuard":null,"tryCatchPattern":"try {\n    aotContribution.applyTo(generationContext, codeGenerator);\n} catch (AotBeanProcessingException ex) {\n    RegisteredBean rb = ex.getRegisteredBean();\n    throw new ConfigurationException(\"AOT failed for bean \" + rb.getBeanName(), ex);\n}","preventionTips":["Run Spring AOT processing in CI to surface codegen failures before native build.","Avoid programmatic instance suppliers and lambda @Bean methods.","Keep Spring modules on aligned versions.","Exclude inherently dynamic beans from AOT processing."],"tags":["spring","aot","native-image","code-generation","bean-definition"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}