Spring Boot: Startup init through @PostConstruct

David Marko  25 January 2017 05:00:00
There are frequent situations when application requires to run custom code while starting up. There are several ways how to do it in Spring Boot, one of my favorites is to create @Component class with method annotated with @PostConstruct . This simple component class is scanned during Spring Boot application start and method annotated by @PostConstruct is run just after all services initialized. Such startup init component can inject / autowire custom managed services and use them for initialization. This way we can, for example, inject Spring Data repository and do some data initialization in our application.

The following code example is minimum class skeleton for our startup init class. We can have several @Component classes in our application with @PostConstruct annotation available. All these are going to be run during Spring Boot start up process.


public class StartUpInit {


CustomServiceExample customServiceExample;


public void init(){

   // init code goes here



See original blog post here.