spring3.0定时器 xml配置和注解方式

简介: 1.xml配置方式web.xml<!-- 配置spring监听器和配置文件路径 -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationCo


1.xml配置方式



web.xml

<!-- 配置spring监听器和配置文件路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

spring配置文件

     
     <beans xmlns="http://www.springframework.org/schema/beans"
     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"
    
    <task:scheduled-tasks>   

        <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>   

    </task:scheduled-tasks> 

    <context:component-scan base-package="com.gy.mytask" />

这里定时每分钟的的零秒开始执行

cron="0 * * * * ?"

java类,方法无返回值

package com.gy.mytask;

import org.springframework.stereotype.Service;

@Service

public class TaskJob {

    public void job1() {

        System.out.println("任务进行中。。。");
        System.out.println(Thread.currentThread().getName());

    }

}


2.注解方式


spring配置文件

    
    
     <context:annotation-config />  <!-- 可以去掉 -->
     
    <context:component-scan base-package="com.gy.mytask" />
    
    <!-- 开启这个配置,spring才能识别@Scheduled注解 -->     

    <task:annotation-driven />


<context:annotation-config/>

作用是显式地向 Spring 容器注册

AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor

PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor  4 BeanPostProcessor。注册这4 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。

因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。


java类

package com.gy.mytask;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Service;


@Service  

public class TaskJob {
    @Scheduled(cron = "0 * * * * ?")
    public void job1() {

        System.out.println("任务进行中。。。");

    }

}



默认是单线程的

pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1
任务进行中。。。
pool-1-thread-1


参考文章:

http://my.oschina.net/u/559635/blog/389558

http://blog.sina.com.cn/s/blog_872758480100wtfh.html


本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1741155

目录
相关文章
|
12天前
|
SQL Java 数据库连接
(自用)Spring常用配置
(自用)Spring常用配置
15 0
|
5天前
|
存储 安全 Java
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
13 0
|
5天前
|
安全 Java 数据库
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(上)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)
28 0
|
6天前
|
安全 Java Spring
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
18 0
|
6天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
24 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
12天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
21 1
|
12天前
|
Java 数据库连接 Spring
简化配置,提高灵活性:Spring中的参数化配置技巧
简化配置,提高灵活性:Spring中的参数化配置技巧
19 0
|
12天前
|
Java Shell 测试技术
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
24 0
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
|
16天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
14 0
|
30天前
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
15 1