I want to know is it possible to run a method only one time in java spring boot with schedule
or spring job
?
I did that with temp variable as switch but I am looking for better and cleaner way.
Asked
Active
Viewed 2,439 times
1

Vadim Kotov
- 8,084
- 8
- 48
- 62

Morteza Malekabadi
- 161
- 1
- 16
-
See https://stackoverflow.com/questions/30347233/spring-scheduling-task-run-only-once – Ori Marko Mar 05 '19 at 08:06
-
One time per application start? One specific point in time? – Mick Mar 05 '19 at 08:08
-
Possible duplicate of [Spring scheduling task - run only once](https://stackoverflow.com/questions/30347233/spring-scheduling-task-run-only-once) – mkjh Mar 05 '19 at 08:09
-
you could also create a bean class and put the code in a `@PostConstruct` annotated method - https://www.baeldung.com/running-setup-logic-on-startup-in-spring – Marc Stroebel Mar 05 '19 at 08:11
-
Explain the use case also, so we can give you better hints – NiVeR Mar 05 '19 at 08:14
1 Answers
2
We can create a component class that implements CommandLineRunner
or ApplicationRunner
, So it will be automatically invoked after the app will start
@Component
public class CommandLineAppStartupRunner implements CommandLineRunner {
@Autowired
private MyService myService;
@Override
public void run(String...args) throws Exception {
myService.save();
}
}

Vinit Solanki
- 1,863
- 2
- 15
- 29