How To Execute method before @PostConstruct work??

I am Trying to update DB Schema whenever application version updated. Some People said It is not a good Function but I wanna try whether I use it or not.

I Succeed Update schema when application booted.

but I have to change the timing that schema updated. and It is between after Bean object DI finished and before @PostConstruct work.

is it impossible, just before @PostConstruct work whether DI is finished or not.

how can I do this?

PS. I have known flyway work similar function compared I am making. but I want to make similar thing by my self.

1 answer

  • answered 2018-11-08 07:01 Mark Bramnik

    When spring bean gets initialized, spring guarantees that all the properties will be injected (by means of applying constructor injection, setter injection or field injection)

    So First of all spring calls bean's constructor Then (if the fields are not set yet) it tries to inject fields And only after that, it calls @PostConstruct

    So you should be able to access the database from the post-construct method of the bean.