Spring, Run task once when application started

I. Domshchikov picture I. Domshchikov · Aug 10, 2016 · Viewed 12.6k times · Source

My application is based on spring boot. I want to create a task which should be run only once after application has been started.

Currently, I am looking into two solutions:

  1. Using @Scheduled and boolean property which should determine whether the logic shold be run or not.

    @Scheduled public void method(){ if(method_run_propery){ //do something; } }

  2. Using Quartz. But I have not used before.

Please, tell me what is the best approach to use in this case.

Answer

Shawn Clark picture Shawn Clark · Aug 10, 2016

Spring has a @PostConstruct annotation to do exactly that. Runs once the bean has been initialized and all dependencies added.