1.spring+task文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <task:annotation-driven/>
<context:component-scan base-package="com.task"/>
</beans>
2.业务类
package com.task; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class TestScheduled { @Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次
public void test(){
for(long i=0;i<1000000000000000000l;i++){
System.out.print((char)i);
if(i%15==0){
System.out.println();
}
}
}
}
启动程序就开始跑了