{"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/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java#L205-L241","documentation":"Thrown by the same init-method invocation path as error 200, but this branch catches a generic Throwable (not InvocationTargetException), meaning the failure is at the reflection/invocation level rather than inside the user's init method body. Typical causes are illegal access, the method disappearing, or an Error (e.g. NoClassDefFoundError, StackOverflowError) raised while setting up the call.","triggerScenarios":"invokeInitMethods throws something that is not an InvocationTargetException - e.g. IllegalAccessException because the init method became inaccessible, NoClassDefFoundError for a type referenced by the method, or an ExceptionInInitializerError from a static block the method triggers.","commonSituations":"Bean class compiled against a different version of a dependency than what is on the runtime classpath; classloader/visibility issues in shaded or modular apps; a static initializer in the bean class failing; reflective access denied under a strict module boundary (Java modules).","solutions":["Read the wrapped Throwable (not just the message) - it identifies the invocation-level failure (NoClassDefFoundError, IllegalAccessException, etc.).","If a missing class/type: align dependency versions between compile and runtime classpath.","If an access error: ensure the init method is non-private or that the package is open/exports the type (Java modules), or make it package-private with makeAccessible allowed.","If a static-init failure: fix the static initializer block that the method transitively triggers."],"exampleFix":"// before\n@PostConstruct\nprivate void init() { /* IllegalAccessException on some classloaders */ }\n// after\n@PostConstruct\nvoid init() { /* package-private, accessible */ }","handlingStrategy":"validation","validationCode":"for (String name : context.getBeanDefinitionNames()) {\n  BeanDefinition bd = context.getBeanFactory().getBeanDefinition(name);\n  if (bd.getBeanClassName() != null) {\n    Class<?> c = ClassUtils.forName(bd.getBeanClassName(), context.getClassLoader());\n    ReflectionUtils.doWithMethods(c, m -> {\n      if (m.isAnnotationPresent(PostConstruct.class) && !Modifier.isAccessible(m)) {\n        log.warn(\"Inaccessible @PostConstruct on {}\", name);\n      }\n    });\n  }\n}","typeGuard":null,"tryCatchPattern":"try { context.refresh(); }\ncatch (BeanCreationException ex) {\n  Throwable cause = ex.getCause(); // not InvocationTargetException - the raw throwable\n  if (cause instanceof NoClassDefFoundError || cause instanceof IllegalAccessException) {\n    log.error(\"Classpath/access mismatch for init method\", cause);\n  }\n  throw ex;\n}","preventionTips":["Pin dependency versions so compile and runtime classpaths match.","For Java modules, ensure packages containing lifecycle methods are open to Spring.","Avoid static initializers with side effects in bean classes."],"tags":["spring","lifecycle","reflection","bean-creation"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}