Spring定时任务

来源:互联网 发布:htc vive unity3d 编辑:程序博客网 时间:2024/05/18 03:29

最近在写一个项目用到定时任务,就把它记录了下来,共大家学习


首先配置applicationContext.xml 文件:

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:task="http://www.springframework.org/schema/task" //主要配置xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/task   
//主要配置
http://www.springframework.org/schema/task/spring-task-3.0.xsd "><bean id="taskTest" class="sp.fun"></bean> <task:scheduled-tasks> <!-- 这里表示的是从第十秒开始 ,每三秒执行一次 (而不是 三分之五 秒执行一次哦~~) --> <task:scheduled ref="taskTest" method="name" cron="10/3 * * * * ?" /> </task:scheduled-tasks> </beans>

然后再web.xml中初始化applicationContext.xml:

<servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet>


下面是我写的小demo做的测试:


public class fun {@Testpublic void name() { String  name1[]={"1","3"};System.err.println(name1[0]);int length = name1.length;if (name1[1].equals("2")) {System.err.println(length+"");}else {System.err.println("----------------");}}}
测试结果如下图:

这样一个完整的配置定时任务配置就完成了:

原创粉丝点击