springrestspring-mvcpostconstruct

Does Spring wait for each @PostConstruct to finish when loading ApplicationContext


I have some logic of loading data from the DB in @PostConstruct method in some of the service classes which takes a long time.

And there are some unrelated controllers which load quite quickly and could potentially start accepting HTTP requests.

So the question, does Spring just wait for each @PostConstruct method to return when loading the ApplicationContext, hence can't accept any requests, or are there any exceptions?

I know, I could load the data from DB asynchronously, but perhaps there're brighter solutions?


Solution

  • In Spring all the REST endpoint are actualized through RequestMappingHandlerMapping class.

    And this kicks in after @PostContruct. So the way you are doing is perfectly fine.