spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务
来源:互联网 发布:linux daemon用户权限 编辑:程序博客网 时间:2024/05/21 22:26
from:http://fanshuyao.iteye.com/blog/2267243
一、计划任务实现类
1、用@Component注解标识计划任务类,这样spring可以自动扫描
2、在方法中使用注解标识要执行的方法:@Scheduled(cron="*/30 * * * * *")
3、周期可以使用cron,或者fixedRate,fixedRate=1000*30表示30秒执行一次,cron请自行百度或看下面的代码的说明
- @Component
- public class SpringTask {
- /**
- * cron表达式:* * * * * *(共6位,使用空格隔开,具体如下)
- * cron表达式:*(秒0-59) *(分钟0-59) *(小时0-23) *(日期1-31) *(月份1-12或是JAN-DEC) *(星期1-7或是SUN-SAT)
- * 注意: 30 * * * * * 表示每分钟的第30秒执行,而(*斜杠30)表示每30秒执行
- *
- * */
- @Scheduled(cron="*/30 * * * * *")
- public void firstTask(){
- System.out.println("==============it is first task!时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
- }
- }
二、需要在spring.xml配置文件中加入命名空间(xmlns:task)
本项目采用了spring4.1,所以用的是4.1
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:task="http://www.springframework.org/schema/task"
- xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
- http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.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.1.xsd">
三、增加包的扫描,一般在spring.xml文件都会有
主要是这个:<context:component-scan base-package="com.spring.*"></context:component-scan>
- <context:component-scan base-package="com.spring.*">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
四、在spring.xml文件中配置计划任务
- <task:annotation-driven scheduler="taskScheduler" mode="proxy"/>
- <task:scheduler id="taskScheduler" pool-size="10"/>
也可以简单点配置,如下:
- <task:annotation-driven />
五、然后启动web项目,就会看到每30秒就打印信息出来。
六、如果需要用到service类,可以注入。
- @Autowired
- private PushRecordService pushRecordService;
记得set get方法
- public PushRecordService getPushRecordService() {
- return pushRecordService;
- }
- public void setPushRecordService(PushRecordService pushRecordService) {
- this.pushRecordService = pushRecordService;
- }
七,注意
同时还要添加一个aopaliaance.jar,否则会报错:noClassDefoundError:org/aopalliance/aop/Advice
地址:http://maven.ibiblio.org/maven2/aopalliance/aopalliance/1.0/
0 0
- spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务
- Spring计划任务(定时任务)
- Spring 定时任务 @Scheduled
- spring scheduled定时任务
- Spring定时任务(@Scheduled)
- spring 定时任务 Scheduled
- Spring @Scheduled 定时任务
- Spring @Scheduled 定时任务
- Spring Boot 计划任务(定时任务)
- spring boot 计划任务,定时任务的注解的使用@Scheduled@EnableScheduling
- spring中的定时任务-@scheduled
- Spring用@Scheduled 定时任务
- spring @Scheduled注解 定时任务
- spring @Scheduled注解 定时任务
- Spring Scheduled实现定时任务
- spring @Scheduled 定时任务使用
- Spring 定时任务scheduled详解
- spring @scheduled 定时任务详解
- leetcode337. House Robber III
- 画笔使用(三)
- 【转】Spring 中jdbcTemplate 实现执行多条sql语句
- ps -aux ps -ef 命令
- 高并发电商平台架构实践
- spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务
- Struts2中关于"There is no Action mapped for namespace / and action name"的总结
- java.lang.IllegalArgumentException: This method supports only the Types Cookie, CacheControl, Entity
- 输出前n个数组合成的第k个数
- html-css-javascript题目集锦-1
- 常见算法及问题场景——动态规划
- 背包九讲的总结笔记(一)
- Mysql Binlog三种格式介绍及分析
- Android实现文件的复制