本文共 2192 字,大约阅读时间需要 7 分钟。
一、创建web项目 项目名称:springtaskdemo1 二、添加支持 1.在lib包添加spring支持 com.springsource.org.aopalliance-1.0.0.jar commons-logging.jar slf4j-api-1.6.1.jar spring-aop-3.2.0.RELEASE.jar spring-beans-3.2.0.RELEASE.jar spring-context-3.2.0.RELEASE.jar spring-context-support-3.2.0.RELEASE.jar spring-core-3.2.0.RELEASE.jar spring-expression-3.2.0.RELEASE.jar spring-tx-3.2.0.RELEASE.jar spring-web-3.2.0.RELEASE.jar 三、添加配置文件 1.在项目中创建conf目录 /conf 2.在conf目录下创建配置文件 配置文件名称:applicationContext.xml 配置文件内容: <?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> </beans> 四、修改web.xml文件,添加如下配置 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> 五、创建任务类 1.在src下创建包 包名:cn.jbit.springtaskdemo1.job 2.在包创建任务类 类名:SpringTaskJob.java 类内容: @Service("springTaskJob") public class SpringTaskJob { @Scheduled(cron="0 21 10 ? * *") public void run(){ System.out.println("annotation run"); } } 六、配置任务类 <!-- 扫描注解 --> <context:annotation-config></context:annotation-config> <!-- 注解所在的包 --> <context:component-scan base-package="cn.jbit.springtaskdemo1.job"></context:component-scan> <task:annotation-driven/> 七、部署并启动服务
本文转自 素颜猪 51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1566587
转载地址:http://qwgfo.baihongyu.com/