SoFunction
Updated on 2025-04-14

@PostConstruct annotation in Java

@PostConstruct Basic:

@PostConstruct annotation Many people think it was provided by Spring. Actually, it is Java's own annotation.

Description of this annotation in Java: @PostConstruct This annotation is used to modify a non-static void() method. The method modified by @PostConstruct will run when the server loads the servlet and will only be executed by the server once. PostConstruct is executed after the constructor and before the init() method.

Usually we will use the @PostConstruct annotation in the Spring framework. The execution order of the annotation method in the entire bean initialization:

Constructor (Construction method) -> @Autowired (Dependency injection) -> @PostConstruct (Annotation method)

Application: Calling methods in dependency injection beans in static methods.

package ;
import ;
import ;
import ;
import ;
@Component
public class MyUtils {
    private static MyUtils          staticInstance = new MyUtils();
    @Autowired
    private MyMethorClassService    myService;
    @PostConstruct
    public void init(){
         = myService;
    }
    public static Integer invokeBean(){
        return (10,20);
    }
}

So how is Spring implemented with the @PostConstruct annotation provided by Java?

You need to learn the interface BeanPostProcessor first:

public interface BeanPostProcessor {
	/**
      * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
	  * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
	  * or a custom init-method). The bean will already be populated with property values.
	  * The returned bean instance may be a wrapper around the original.
      *
      * Any bean instantiated and the bean has been populated (filled with properties) will call back this method
      *
      */
	Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
	/**
	  * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
	  * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
	  * or a custom init-method). The bean will already be populated with property values.
	  * The returned bean instance may be a wrapper around the original.
      *
      * Any bean instantiated and the bean has been populated (filled with properties) will call back this method
      *
      */
	Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

So where do Spring initialization callbacks these methods?

(...);
    ();
       (beanName);
          ();
            (....)
                populateBean(beanName, mbd, instanceWrapper);
                initializeBean(...)
                 //Calling the() method                  applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
                 //Calling the() method                  applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);

BeanPostProcessor has an implementation class CommonAnnotationBeanPostProcessor, which specializes in handling @PostConstruct @PreDestroy annotation.

CommonAnnotationBeanPostProcessorParent classInitDestroyAnnotationBeanPostProcessor()
 ()
    ()
        // Assemble life cycle metadata        ()
            // Method to find @PostConstruct annotation            
            // Find @PreDestroy annotation method            
 // Reflection call (bean, beanName);    

This is all about this article about @PostConstruct annotation in Java. For more related Java @PostConstruct annotation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!