>  기사  >  Java  >  스프링 작업 예약 작업 구현 예

스프링 작업 예약 작업 구현 예

高洛峰
高洛峰원래의
2017-02-07 15:35:401559검색

1. spring 관련 jar 패키지 소개:

spring task 定时任务实现示例

2. web.xml에서 spring 구성

<listener>
  <description>Spring监听器</description>
  <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>

3. 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: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.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/task
  http://www.springframework.org/schema/task/spring-task-3.0.xsd"
  default-lazy-init="false">
 
  <!-- 注解方式 -->
  <context:annotation-config />
  <context:component-scan base-package="com.test.task" />
  <task:annotation-driven/>
   
  <!-- XML方式 -->
  <!-- <bean name="testTask" class="com.test.task.TestTask" lazy-init="false"></bean>
  <task:scheduled-tasks> 
    <task:scheduled ref="testTask" method="print" cron="0/5 * * * * ?"/> 
  </task:scheduled-tasks> -->
 
</beans>

에서 리스너를 구성합니다. 4. 엔터티 클래스

package com.test.task;
 
import java.text.DateFormat;
import java.util.Date;
 
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
@Component
public class TestTask {
  @Scheduled(cron = "*/5 * * * * ?")
  public void print(){
    String time = DateFormat.getDateTimeInstance().format(new Date());
    System.out.println("定时器触发打印"+time);
  }
}

를 작성합니다. 5. 프로젝트 디렉터리:

실행 결과:

spring task 定时任务实现示例

위 내용은 모두의 학습에 도움이 되기를 바라며, PHP 중국어도 구독해 주시길 바랍니다. 웹사이트.

봄 작업 예약 작업 구현 예제와 관련된 더 많은 기사를 보려면 PHP 중국어 웹사이트를 주목하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.