一、Java创建定时任务(SpringMVC工程)
- 创建类,加入@Schedule注解
- 创建定时启动方法,加入@Scheduled(cron="0 0/1 * * * ? ")注解
- 如果任务只需要启动一次,可以加入相关变量进行控制
- 修改springContext配置
- 加入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
- 加入<task:executor id="executor" pool-size="10" />
- 加入<task:scheduler id="scheduler" pool-size="10" />
- 加入<task:annotation-driven executor="executor" scheduler="scheduler" />
- 加入扫描<context:component-scan base-package="com.martinWeb.annuniator.Schedule"></context:component-scan>
- (如果定时任务启动两次)修改web.xml配置
- 删除
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
- 删除