package com.yutou.qqbot.utlis; import com.yutou.qqbot.models.Model; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; @Component public class ApplicationInit implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { new Timer().schedule(new TimerTask() { private String oldTime = ""; @Override public void run() { try { String time = new SimpleDateFormat("HH:mm:ss").format(new Date()); if (time.equals(oldTime)) { return; } oldTime=time; for (Class model : Model.classList) { try { Model useModel= (Model) model.getDeclaredConstructor().newInstance(); useModel.onTime(time); } catch (Exception e) { e.printStackTrace(); } } }catch (Exception e){ e.printStackTrace(); } } },0, 1000); } }