Creating a Spring Framework programming task?

I need to create a task on the fly in my application. How can i do this? I can get the scheduler with the @autowired annotation, but the scheduler accepts Runnable objects. I need to provide Spring objects, so my tasks can also use @autowired annotation.

 @Autowired private TaskScheduler taskScheduler; 
+7
source share
1 answer

You just need to wrap the target in Runnable and imagine that:

 private Target target; // this is a Spring bean of some kind @Autowired private TaskScheduler taskScheduler; public void scheduleSomething() { Runnable task = new Runnable() { public void run() { target.doTheWork(); } }; taskScheduler.scheduleWithFixedDelay(task, delay); } 
+16
source

All Articles