spring-projects/spring-framework · error · BeanCreationException

Invocation of init method failed

Error message

Invocation of init method failed

What it means

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.

Source

Thrown at spring-beans/src/main/java/org/springframework/beans/factory/annotation/InitDestroyAnnotationBeanPostProcessor.java:220

		metadata.checkInitDestroyMethods(beanDefinition);
		return metadata;
	}

	private static String[] safeMerge(String @Nullable [] existingNames, Collection<LifecycleMethod> detectedMethods) {
		Stream<String> detectedNames = detectedMethods.stream().map(LifecycleMethod::getIdentifier);
		Stream<String> mergedNames = (existingNames != null ?
				Stream.concat(detectedNames, Stream.of(existingNames)) : detectedNames);
		return mergedNames.distinct().toArray(String[]::new);
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		LifecycleMetadata metadata = findLifecycleMetadata(bean.getClass());
		try {
			metadata.invokeInitMethods(bean, beanName);
		}
		catch (InvocationTargetException ex) {
			throw new BeanCreationException(beanName, "Invocation of init method failed", ex.getTargetException());
		}
		catch (Throwable ex) {
			throw new BeanCreationException(beanName, "Failed to invoke init method", ex);
		}
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	@Override
	public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
		LifecycleMetadata metadata = findLifecycleMetadata(bean.getClass());
		try {
			metadata.invokeDestroyMethods(bean, beanName);
		}

View on GitHub (pinned to e8729d0438)

Solutions

  1. Inspect the cause chain (getCause()/getRootCause()) of the BeanCreationException - the real exception is the unwrapped target, not this wrapper.
  2. Set a breakpoint or add logging inside the failing @PostConstruct method to capture the original stack trace.
  3. Fix the underlying issue in the init method (null collaborator -> add @Autowired/@DependsOn, missing property -> define it, failing external call -> guard/validate).
  4. Move unsafe logic out of @PostConstruct into a dedicated method invoked after context start if it needs a fully-initialized environment.

Example fix

// before
@PostConstruct
public void init() {
  this.client.connect(); // throws if url not injected yet
}
// after
@PostConstruct
public void init() {
  Assert.notNull(this.url, "url required");
  this.client.connect();
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
  context.refresh();
} catch (BeanCreationException ex) {
  Throwable root = ex.getMostSpecificCause();
  if (root.getStackTrace()[0].getMethodName().equals("<init>") || isInitMethod(root)) {
    log.error("Init method failed on bean {}: {}", ex.getBeanName(), root);
  }
  throw ex;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04). Data as JSON: /data/errors/cb24f0196f6e80a4.json. Report an issue: GitHub.