{"record":{"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/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java#L202-L238","documentation":"Thrown by InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization() when a custom init-annotated method (e.g., @PostConstruct or a custom init annotation) is invoked reflectively and throws an exception. The InvocationTargetException wraps the target exception; this BeanCreationException unwraps it via ex.getTargetException() so the user sees the original business exception as the cause.","triggerScenarios":"A method annotated with the configured init annotation (default: @PostConstruct via CommonAnnotationBeanPostProcessor, or a custom annotation set via setInitAnnotationType) throws any exception during bean initialization. The reflective Method.invoke() wraps it in InvocationTargetException, which is caught at line 219.","commonSituations":"@PostConstruct method connecting to a database that is not yet available. Init method performing validation that fails on bad configuration. A startup task that throws because of missing external resources (files, services, etc.).","solutions":["Inspect the target exception (ex.getCause() or ex.getTargetException()) — it is the real business exception from your init method.","Add error handling inside the init method itself for expected failures (retry logic, fallback values).","Ensure external resources required by the init method are available before context refresh."],"exampleFix":"// before\n@PostConstruct\npublic void init() {\n    this.connection = dataSource.getConnection(); // throws if DB down\n}\n\n// after\n@PostConstruct\npublic void init() {\n    try {\n        this.connection = dataSource.getConnection();\n    } catch (SQLException e) {\n        logger.warn(\"DB unavailable at init, will retry\", e);\n    }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    context.getBean(MyBean.class);\n} catch (BeanCreationException ex) {\n    if (ex.getMessage().contains(\"Invocation of init method failed\")) {\n        Throwable target = ex.getCause(); // the real business exception\n        // handle init failure: retry, fallback, or fail fast\n    }\n}","preventionTips":["Handle expected failures inside @PostConstruct methods with try-catch.","Ensure external resources (DB, files, services) are available before context refresh.","Log the target exception clearly so the root cause of init failures is visible."],"tags":["spring","lifecycle","init-method","bean-creation"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}