Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,可以用它来实现很多的功能,其中比较简单就是定时器。下面我们基于xml配置来实现Spring定时任务。
定时任务组件分为三个:调度器、任务以及执行时间点。我们需要引入bean、context、core三个spring包。
application.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
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.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- 创建一个调度器 -->
<task:scheduler id="schedualer"/>
<!-- 配置任务类的bean -->
<bean id="xmlTask" class="wokao666.club.spring_task.tasks.XmlTask"></bean>
<task:scheduled-tasks scheduler="schedualer">
<!-- 每2秒执行一次 -->
<task:scheduled ref="xmlTask" method="say" cron="0/2 * * * * ?"/>
</task:scheduled-tasks>
</beans>
XmlTask.java
package wokao666.club.spring_task.tasks;
import java.text.SimpleDateFormat;
import wokao666.club.spring_task.util.DateFormatter;
/**
* 基于XML的spring定时任务
*/
public class XmlTask {
public void say() {
SimpleDateFormat format = DateFormatter.getDateFormatter();
System.err.println(format.format(System.currentTimeMillis()) + " I am spring xml-based task!");
}
}
DateFormatter.java
package wokao666.club.spring_task.util;
import java.text.SimpleDateFormat;
/**
* 日期格式
* @author hjw
*
*/
public class DateFormatter {
private static volatile SimpleDateFormat formater = null;
private DateFormatter() {}
public static SimpleDateFormat getDateFormatter() {
if(null == formater) {
synchronized (DateFormatter.class) {
if(null == formater) {
formater = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
}
}
}
return formater;
}
}
App.java
package wokao666.club.spring_task;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*
*/
public class App
{
private static ClassPathXmlApplicationContext ctx;
public static void main( String[] args ) {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
程序输出:
六月 10, 2018 10:47:11 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2328c243: startup date [Sun Jun 10 22:47:11 CST 2018]; root of context hierarchy
六月 10, 2018 10:47:11 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
六月 10, 2018 10:47:12 下午 org.springframework.scheduling.concurrent.ExecutorConfigurationSupport initialize
信息: Initializing ExecutorService 'schedualer'
2018-06-10 10:47:14 I am spring xml-based task!
2018-06-10 10:47:16 I am spring xml-based task!
2018-06-10 10:47:18 I am spring xml-based task!
好了,上面就是基于xml配置实现Spring定时任务。对其中的代码有不理解的小伙伴,可以到动力节点在线的Spring视频教程查看对应的课程,有老师作出详细的讲解哦。
提枪策马乘胜追击04-21 20:01
代码小兵92504-17 16:07
代码小兵98804-25 13:57
杨晶珍05-11 14:54