spring 使用注解来调度定时任务
2015-08-05 13:15
567 查看
1.在需要加载spring的配置文件里spring.xml / applicationContext.xml 添加
[html] view
plaincopy
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
2.配置自动调度的包和定时开关
[html] view
plaincopy
<!--
使用注解 -->
<context:annotation-config />
<!-- 自动调度需要扫描的包 -->
<context:component-scan base-package="*" />
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
3.配置调度和注解调度
[html] view
plaincopy
<!-- 配置调度 需要在类名前添加 @Service -->
<!--
<task:scheduled-tasks>
<task:scheduled ref="demoTask" method="myTestWork" cron="0/10 * * * * ?"/>
</task:scheduled-tasks>
-->
<!-- 不通过配置调度,需要在类名前 @Component/@Service,在方法名 前添加@Scheduled(cron="0/5 * * * * ? ")-->
4.在web.xml配置里添加启动时要扫描的配置文件和监听
[html] view
plaincopy
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
5.添加调度的包
6.类测试
[java] view
plaincopy
@Service
public class demo {
@Scheduled(cron="0/5 * * * * ? ")
public void myTestWork(){
System.out.println("ssss");
}
}
[html] view
plaincopy
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
2.配置自动调度的包和定时开关
[html] view
plaincopy
<!--
使用注解 -->
<context:annotation-config />
<!-- 自动调度需要扫描的包 -->
<context:component-scan base-package="*" />
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
3.配置调度和注解调度
[html] view
plaincopy
<!-- 配置调度 需要在类名前添加 @Service -->
<!--
<task:scheduled-tasks>
<task:scheduled ref="demoTask" method="myTestWork" cron="0/10 * * * * ?"/>
</task:scheduled-tasks>
-->
<!-- 不通过配置调度,需要在类名前 @Component/@Service,在方法名 前添加@Scheduled(cron="0/5 * * * * ? ")-->
4.在web.xml配置里添加启动时要扫描的配置文件和监听
[html] view
plaincopy
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
5.添加调度的包
6.类测试
[java] view
plaincopy
@Service
public class demo {
@Scheduled(cron="0/5 * * * * ? ")
public void myTestWork(){
System.out.println("ssss");
}
}
相关文章推荐
- java包装类与常量池
- spring结合Servlet
- org.eclipse.ui.activities过滤不适用的扩展点
- 【Spring】Spring IOC原理及源码解析之scope=request、session
- Struts学习之手动验证
- Eclipse连接JDBC数据库遇到的问题
- Struts学习之文件上传
- Struts学习之模型驱动
- 黑马程序员_Java基础:JDK1.5后的新特性:自动拆装箱,以及注意事项
- java异常
- Java和C#的socket通信相关(转)
- Struts学习之类型转换
- java实现打印功能并控制打印方向
- Java RandomAccessFile用法
- Java 重载、重写、多态
- Struts学习之自定义拦截器
- Java实现排序算法之堆排序
- java二叉树非递归之中序遍历
- Java类的变量初始化顺序
- java二叉树非递归之前序遍历