以编程方式创建Spring Framework任务? [英] Creating Spring Framework task programmatically?
本文介绍了以编程方式创建Spring Framework任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在我的应用中动态创建任务。我怎样才能做到这一点?我可以使用 @autowired
注释来获取调度程序,但调度程序需要 Runnable
对象。我需要提供Spring对象,以便我的任务也可以使用 @autowired
注释。
I need to create task on the fly in my app. How can I do that? I can get scheduler with @autowired
annotation, but scheduler takes Runnable
objects. I need to give Spring objects, so that my tasks can use @autowired
annotation too.
@Autowired private TaskScheduler taskScheduler;
推荐答案
您只需要将目标对象包装在 Runnable
,并提交:
You just need to wrap your target object in a Runnable
, and submit 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);
}
这篇关于以编程方式创建Spring Framework任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文