在spring配置文件中 添加
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
<context:component-scan base-package="com.yafco.yjzx.controller" /> 设定范围
<task:annotation-driven />
<!-- 打开springMVC定时器开关 -->
<!-- 设置需要定时启动的方法 -->
<bean id="ceshi" class="com.yafco.yjzx.controller.cs.Ceshi" scope="singleton"/>
<task:scheduled-tasks>
<task:scheduled ref="ceshi" method="taskCycle" cron="0/5 * * * * ?" /> <!-- 每隔五秒执行一次 -->
</task:scheduled-tasks>
通过spring控制,java文件不用改动。