Home

Detailed Explanation of the PostConstruct Annotation

Li

Li Wei

August 5, 20253 min read

@PostConstruct Annotation Explained

Overview

@PostConstruct is an annotation in Java used to mark a method that will be automatically invoked after dependency injection is complete. It ensures that once the class constructor has finished executing, all dependencies have been injected before the annotated method runs.

Use Cases

In the code example below, @PostConstruct marks the init() method, ensuring that after the ThreadPoolUtils class is fully initialized, a thread pool is created and configured:

Common scenarios for using @PostConstruct include:

  • Initializing complex objects: when an object requires elaborate setup after creation
  • Resource allocation: allocating or initializing resources such as thread pools, database connection pools, etc.
  • Data pre‑loading: loading data during application startup
  • Validating dependencies: making sure all required dependencies have been correctly injected
  • Starting background tasks: launching tasks that should run after the application starts

How It Works

The lifecycle of @PostConstruct works as follows:

  • The container instantiates the bean
  • The container injects the bean’s dependencies
  • The container calls the method annotated with @PostConstruct
  • The bean is ready for use

In a Java EE environment, @PostConstruct is handled by CommonAnnotationBeanPostProcessor, which implements the BeanPostProcessor interface and is invoked during bean initialization.

Within Spring’s bean lifecycle, the execution order of a @PostConstruct method is:

  • Instantiate the bean (call the constructor)
  • Populate properties (dependency injection)
  • If the bean implements BeanNameAware, call the setBeanName() method
  • If the bean implements BeanFactoryAware, call the setBeanFactory() method
  • If the bean implements ApplicationContextAware, call the setApplicationContext() method
  • Call the BeanPostProcessor’s postProcessBeforeInitialization method
  • Execute the method annotated with @PostConstruct
  • If the bean implements InitializingBean, call the afterPropertiesSet() method
  • Call the method specified by the bean’s init-method attribute
  • Call the BeanPostProcessor’s postProcessAfterInitialization method
  • The bean is now usable
  • When the container shuts down, if the bean implements DisposableBean, call the destroy() method
  • Call the method specified by the bean’s destroy-method attribute

Considerations

  • A @PostConstruct method should not throw checked exceptions
  • Only one method per class may be annotated with @PostConstruct
  • The method must have no parameters
  • The method’s return type must be void
  • The method cannot be static
  • The method may be final
  • The method may be private, protected, package‑private, or public

Originally written by Li Wei (李唯_) and published in Chinese on 后端技术栈全书 (Full-Stack Backend Engineering). Translated and adapted for DriftSeas with permission.

Keep reading

More related articles from DriftSeas.