1:spring的任務(wù)調(diào)度配置
@Service
public class timeTasks1{
public void execute() {
Thread t = new Thread(new Runnable(){
public void run(){
System.out.println(“執(zhí)行任務(wù)”);
}});
t.start();
}
}
<context:component-scan base-package="com.test"/>
<task:scheduled-tasks>
<task:scheduled ref="timeTasks1" method="execute" initial-delay="5000" fixed-delay="3600000"/>
<task:scheduled ref="timeTasks2" method="exchange" cron="0 59 23 * * ?" />
</task:scheduled-tasks>
第一個任務(wù)表示程序啟動5s后調(diào)用timeTasks1類中的execute方法,然后每隔一個小時再調(diào)用execute一次
第三個任務(wù)表示每天的23點59分調(diào)用timeTasks2類中的exchange方法
更加全面的任務(wù)調(diào)度框架可以選擇quartz
2.spring 的事件監(jiān)聽:
事件類 extends org.springframework.context.ApplicationEvent;
監(jiān)聽類 implements org.springframework.context.ApplicationListener<ContextRefreshedEvent>
@EventListener事件監(jiān)聽,定義事件監(jiān)聽的Bean,