{"record":{"id":"ca82931f2a543192","repo":"spring-projects/spring-framework","slug":"failed-to-invoke-init-method","errorCode":null,"errorMessage":"Failed to invoke init method","messagePattern":"Failed to invoke init method","errorType":"exception","errorClass":"BeanCreationException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java","lineNumber":223,"sourceCode":"\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}\n\t\tcatch (InvocationTargetException ex) {\n\t\t\tString msg = \"Destroy method on bean with name '\" + beanName + \"' threw an exception\";\n\t\t\tif (logger.isDebugEnabled()) {","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java#L205-L241","documentation":"Thrown by InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization() when invoking an init-annotated method fails with a Throwable that is NOT an InvocationTargetException — i.e., a reflective or infrastructure error rather than a business exception from the method body. Examples include IllegalAccessException (method not accessible) or generic Error types.","triggerScenarios":"The init method cannot be invoked reflectively due to access restrictions (private method on a module path without --add-opens), or the invocation throws an Error (StackOverflowError, OutOfMemoryError, etc.). The catch(Throwable) at line 222 wraps these non-InvocationTargetException cases.","commonSituations":"Java module system blocking reflective access to a private init method (InaccessibleObjectException). Init method annotated but the method was removed or renamed in a class reload scenario. JVM-level errors during init.","solutions":["If the cause is InaccessibleObjectException or IllegalAccessException, ensure the method is accessible (public, or add --add-opens JVM flags).","Inspect the cause throwable — if it is an Error, address the JVM-level issue.","Verify the init annotation is on the correct method that still exists with the expected signature."],"exampleFix":"// before\n@PostConstruct\nprivate void init() { ... } // private on module path\n\n// after\n@PostConstruct\npublic void init() { ... } // accessible","handlingStrategy":"try-catch","validationCode":"null","typeGuard":null,"tryCatchPattern":"try {\n    context.getBean(MyBean.class);\n} catch (BeanCreationException ex) {\n    if (ex.getMessage().contains(\"Failed to invoke init method\")) {\n        Throwable cause = ex.getCause(); // IllegalAccessException, Error, etc.\n        if (cause instanceof IllegalAccessException) {\n            // fix method visibility or add --add-opens\n        }\n    }\n}","preventionTips":["Make init/destroy methods accessible (public, or add --add-opens for modules).","Verify the annotated method still exists with the expected no-arg signature after refactoring.","Run context refresh in a try-catch during development to catch invocation failures early."],"tags":["spring","lifecycle","init-method","reflection"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}