{"id":"cb24f0196f6e80a4","repo":"spring-projects/spring-framework","slug":"invocation-of-init-method-failed","errorCode":null,"errorMessage":"Invocation of init method failed","messagePattern":"Invocation of init method failed","errorType":"exception","errorClass":"BeanCreationException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java","lineNumber":220,"sourceCode":"\t\tmetadata.checkInitDestroyMethods(beanDefinition);\n\t\treturn metadata;\n\t}\n\n\tprivate static String[] safeMerge(String @Nullable [] existingNames, Collection<LifecycleMethod> detectedMethods) {\n\t\tStream<String> detectedNames = detectedMethods.stream().map(LifecycleMethod::getIdentifier);\n\t\tStream<String> mergedNames = (existingNames != null ?\n\t\t\t\tStream.concat(detectedNames, Stream.of(existingNames)) : detectedNames);\n\t\treturn mergedNames.distinct().toArray(String[]::new);\n\t}\n\n\t@Override\n\tpublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {\n\t\tLifecycleMetadata metadata = findLifecycleMetadata(bean.getClass());\n\t\ttry {\n\t\t\tmetadata.invokeInitMethods(bean, beanName);\n\t\t}\n\t\tcatch (InvocationTargetException ex) {\n\t\t\tthrow new BeanCreationException(beanName, \"Invocation of init method failed\", ex.getTargetException());\n\t\t}\n\t\tcatch (Throwable ex) {\n\t\t\tthrow new BeanCreationException(beanName, \"Failed to invoke init method\", ex);\n\t\t}\n\t\treturn bean;\n\t}\n\n\t@Override\n\tpublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {\n\t\treturn bean;\n\t}\n\n\t@Override\n\tpublic void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {\n\t\tLifecycleMetadata metadata = findLifecycleMetadata(bean.getClass());\n\t\ttry {\n\t\t\tmetadata.invokeDestroyMethods(bean, beanName);\n\t\t}","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java#L202-L238","documentation":"Thrown by InitDestroyAnnotationBeanPostProcessor (the @PostConstruct/@PreDestroy processor) when a bean's init method runs but the target method itself throws an application exception. Spring catches the InvocationTargetException, unwraps the original cause via getTargetException(), and re-wraps it as a BeanCreationException so the real failure is the cause chain's root. The bean is not created and context startup (or refresh) fails.","triggerScenarios":"A method annotated with @PostConstruct (or the configured init annotation) executes during postProcessBeforeInitialization and throws any exception. The thrown object becomes the targetException of the reflected call, which Spring unwraps here.","commonSituations":"Init method that opens a DB/network connection with wrong credentials, dereferences a not-yet-injected collaborator, calls a missing property, or hits a NullPointerException because ordering/depends-on is wrong. Common after a refactor that adds a required field the init method reads before injection completes.","solutions":["Inspect the cause chain (getCause()/getRootCause()) of the BeanCreationException - the real exception is the unwrapped target, not this wrapper.","Set a breakpoint or add logging inside the failing @PostConstruct method to capture the original stack trace.","Fix the underlying issue in the init method (null collaborator -> add @Autowired/@DependsOn, missing property -> define it, failing external call -> guard/validate).","Move unsafe logic out of @PostConstruct into a dedicated method invoked after context start if it needs a fully-initialized environment."],"exampleFix":"// before\n@PostConstruct\npublic void init() {\n  this.client.connect(); // throws if url not injected yet\n}\n// after\n@PostConstruct\npublic void init() {\n  Assert.notNull(this.url, \"url required\");\n  this.client.connect();\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  context.refresh();\n} catch (BeanCreationException ex) {\n  Throwable root = ex.getMostSpecificCause();\n  if (root.getStackTrace()[0].getMethodName().equals(\"<init>\") || isInitMethod(root)) {\n    log.error(\"Init method failed on bean {}: {}\", ex.getBeanName(), root);\n  }\n  throw ex;\n}","preventionTips":["Keep @PostConstruct methods defensive: assert required collaborators are non-null before use.","Use @DependsOn to guarantee ordering when an init method reads another bean's state.","Avoid heavy/external work in @PostConstruct; move it to ApplicationRunner/CommandLineRunner after context start."],"tags":["spring","lifecycle","postconstruct","bean-creation"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}