---
title: "spring 定时器配置"
author: "e_e"
date: "2016-09-08T11:39:37Z"
tags: []
categories: ["互联网"]
slug: "spring"
draft: false
lastmod: "2016-09-08T11:39:37Z"
id: 2323291
url: "https://e-e.iteye.com/blog/2323291"
---

1.spring配置文件中引入域名空间，添加标签

xmlns:task="http://www.springframework.org/schema/task"

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd

&lt;task:annotation-driven/&gt; 

 

2\. 组件实现实例，最好写上@Lazy(false)

 

@Component

@Lazy(false)

public class AutoReConnectionTask {

    private static final Logger LOG = Logger.getLogger(AutoReConnectionTask.class);

    

    @Autowired

    ReConnectService reConnectService ; 

    

    @Scheduled(cron="0 0 1 * * ?") //秒 分 时 日 月 星期 年

    public void taskUserOrgBrankingList(){

        try {

            LOG.info("定时任务 AutoReConnectionTask 刷新！");

            int ret = reConnectService.reConnection();

            if(ret == 1 ){

                LOG.info("AutoReConnectionTask 刷新成功");

            }else{

                LOG.info("AutoReConnectionTask 刷新异常");

            }

        } catch (Exception e) {

            LOG.error("定时器执行失败，orgRandkingList 到 user\_orgRandkingList表 ", e);

        }

    }

}

3.组件扫描

&lt;context:component-scan base-package="com.chengshu.Controller"/&gt;