0
假設有Spring引導Web應用程序,其中有2個映射爲@Controller和@Service的類。服務被注入到控制器的領域。我需要我的服務每秒運行一次任務來更新一些外部數據。這段代碼有什麼問題嗎?春季服務週期性任務
@Component
public class MyService implements Runnable{
public MyService() {
new Thread(this).start();
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
// operations here
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
你見過https://spring.io/guides/gs/scheduling-tasks/嗎? – jonrsharpe
您可以使用彈簧石英做到這一點 –