spring-projects/spring-framework · error · BeanCreationException

Invocation of init method failed

Error message

Invocation of init method failed

What it means

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.

Solutions

  1. Inspect the target exception (ex.getCause() or ex.getTargetException()) — it is the real business exception from your init method.
  2. Add error handling inside the init method itself for expected failures (retry logic, fallback values).
  3. Ensure external resources required by the init method are available before context refresh.

Example fix

// before
@PostConstruct
public void init() {
    this.connection = dataSource.getConnection(); // throws if DB down
}

// after
@PostConstruct
public void init() {
    try {
        this.connection = dataSource.getConnection();
    } catch (SQLException e) {
        logger.warn("DB unavailable at init, will retry", e);
    }
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
    context.getBean(MyBean.class);
} catch (BeanCreationException ex) {
    if (ex.getMessage().contains("Invocation of init method failed")) {
        Throwable target = ex.getCause(); // the real business exception
        // handle init failure: retry, fallback, or fail fast
    }
}

Prevention

When it happens

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

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

Related errors


AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09). Data as JSON: /api/errors/cb24f0196f6e80a4. Report an issue: GitHub.

Appendix: 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 69bf83ad71)